demux_lavf: get total duration from per-track durations as fallback

Apparently fixes youtube mp4 streams if avformat_find_stream_info() is
not called.

Keeping audio/video track and other track durations separate is for
the sake of embedded subtitle streams, where we want to include the
duration of overlong subtitle streams (I think).
This commit is contained in:
wm4 2017-03-01 16:13:15 +01:00
parent ee9c850a00
commit 9080e4d468
1 changed files with 19 additions and 2 deletions

View File

@ -1039,8 +1039,25 @@ static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
switch (cmd) {
case DEMUXER_CTRL_GET_TIME_LENGTH:
if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
return DEMUXER_CTRL_DONTKNOW;
if (priv->avfc->duration <= 0) {
double total_duration = 0;
double av_duration = 0;
for (int n = 0; n < priv->avfc->nb_streams; n++) {
AVStream *st = priv->avfc->streams[n];
if (st->duration <= 0)
continue;
double f_duration = st->duration * av_q2d(st->time_base);
total_duration = MPMAX(total_duration, f_duration);
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ||
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
av_duration = MPMAX(av_duration, f_duration);
}
double duration = av_duration > 0 ? av_duration : total_duration;
if (duration <= 0)
return DEMUXER_CTRL_DONTKNOW;
*(double *)arg = duration;
return DEMUXER_CTRL_OK;
}
*((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
return DEMUXER_CTRL_OK;