通过命令 ffplay http://192.168.2.36/files/video/fail_ori.mp4 播放一个HTTP server上的MP4视频时, 提示错误partial file , 而这样的视频一般是手机存储的.
具体原因是 ff_configure_buffers_for_index 这个函数处理的一个bug . 通过调试ffmpeg代码发现. 这个问题产生和track 存储
每个sample的offset 有关.
通过MP4info(下载地址)分析sample的offset 发现. 出问题的视频非交织存储音视频sample如图:
而正常视频如图:
解决办法就是将非交织视频 重新 remux. 方法很多如 :
ffmpeg -i e:\a.mp4 -c copy -movflags faststart e:\\success.mp4
-movflags faststart : 这个是将moov移动到 mp4头部,和remux没有关系. 这样处理也是通过HTTP 流式播放必须要做的一步.
现在给出出问题的代码位置:红色标记的地方.
根据ffmpeg 文件头中定义,ffio_set_buf_size 这个函数需要在 任何io操作之前调用, 而此时 io已经调用并且 buffer中已经填充内容,此时再次调用导致 buffer 内容被清空.导致后面调用avio_seek失败.
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
{
int ist1, ist2;
int64_t pos_delta = 0;
int64_t skip = 0;
//We could use URLProtocol flags here but as many user applications do not use URLProtocols this would be unreliable
const char *proto = avio_find_protocol_name(s->url);
if (!proto) {
av_log(s, AV_LOG_INFO,
"Protocol name not provided, cannot determine if input is local or "
"a network protocol, buffers and access patterns cannot be configured "
"optimally without knowing the protocol\n");
}
if (proto && !(strcmp(proto, "file") && strcmp(proto, "pipe") && strcmp(proto, "cache")))
return;
return;
for (ist1 = 0; ist1 < s->nb_streams; ist1++) {
AVStream *st1 = s->streams[ist1];
for (ist2 = 0; ist2 < s->nb_streams; ist2++) {
AVStream *st2 = s->streams[ist2];
int i1, i2;
if (ist1 == ist2)
continue;
for (i1 = i2 = 0; i1 < st1->nb_index_entries; i1++) {
AVIndexEntry *e1 = &st1->index_entries[i1];
int64_t e1_pts = av_rescale_q(e1->timestamp, st1->time_base, AV_TIME_BASE_Q);
skip = FFMAX(skip, e1->size);
for (; i2 < st2->nb_index_entries; i2++) {
AVIndexEntry *e2 = &st2->index_entries[i2];
int64_t e2_pts = av_rescale_q(e2->timestamp, st2->time_base, AV_TIME_BASE_Q);
if (e2_pts - e1_pts < time_tolerance)
continue;
pos_delta = FFMAX(pos_delta, e1->pos - e2->pos);
break;
}
}
}
}
pos_delta *= 2;
/* XXX This could be adjusted depending on protocol*/
if (s->pb->buffer_size < pos_delta && pos_delta < (1<<24)) {
av_log(s, AV_LOG_VERBOSE, "Reconfiguring buffers to size %"PRId64"\n", pos_delta);
ffio_set_buf_size(s->pb, pos_delta); // 错误出现位置.
s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, pos_delta/2);
}
if (skip < (1<<23)) {
s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, skip);
}
}