美文网首页
ffmpeg的细节问题

ffmpeg的细节问题

作者: 大飞_2048 | 来源:发表于2021-04-06 16:49 被阅读0次

AVFormatContext
start_time 参数意义
:stream的开始时间。这个是时间是音视频帧最小时间戳。

一、ffmpeg中代码实现:

//获取每个流Streams中第一音频或者视频帧时间戳:


image.png

static void update_initial_timestamps(AVFormatContext *s, int stream_index,
                                      int64_t dts, int64_t pts, AVPacket *pkt)
 if (st->start_time == AV_NOPTS_VALUE) {  //取各个stream开始时间
        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
            st->start_time = pts; 
        }
        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
            st->start_time += av_rescale_q(st->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base);

    }

开始给AVFormatContext的start_time赋值
小算法 比较相邻的stream 开始时间,最后选择最小的给AVFormatContext赋值

static void update_stream_timings(AVFormatContext *ic){
     start_time = INT64_MAX;
    start_time_text = INT64_MAX;
  ...
for (i = 0; i < ic->nb_streams; i++) {
        AVStream *st = ic->streams[i];
        int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
                      st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
        if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
            start_time1 = av_rescale_q(st->start_time, st->time_base,
                                       AV_TIME_BASE_Q);
            if (is_text)
                start_time_text = FFMIN(start_time_text, start_time1); // 取小值
            else
                start_time = FFMIN(start_time, start_time1); // 取小值
           ...
          }
      } // end of for 

// 比较
    if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
        start_time = start_time_text;
    else if (start_time > start_time_text)
        av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);

...
    if (start_time != INT64_MAX) {
        ic->start_time = start_time; // 赋值
...
      }
}

二、ffprobe看一个具体的例子,关于这个时间start_time

所有帧数据的音频时间戳:

  ffprobe -show_packets -select_streams a https://yaya-resource.cdnjtzy.com/video/005f382e97fbd5b6715aae720caecec6_1280x720_20210324230522-000.ts  | grep pts_time
image.png

所有视频帧的时间戳:

ffprobe -show_packets -select_streams a https://yaya-resource.cdnjtzy.com/video/005f382e97fbd5b6715aae720caecec6_1280x720_20210324230522-000.ts  | grep pts_time
image.png

三、去掉start_time

       -max_delay 0

ffmpeg -i **.mp4 -f mpegts -max_delay 0 aaa.ts
然后再切流
ffmpeg -i record.ts -max_muxing_queue_size 9999 -vcodec copy -g 40 -c:a copy -strict -2 -f hls -hls_time 4 -hls_list_size 0 -bsf:v h264_mp4toannexb record.m3u8

代码实现 image.png

相关文章

网友评论

      本文标题:ffmpeg的细节问题

      本文链接:https://www.haomeiwen.com/subject/jnjskltx.html