【问题标题】:Confusion about PTS in video files and media streams对视频文件和媒体流中的 PTS 感到困惑
【发布时间】:2015-01-04 05:11:24
【问题描述】:

文件中特定帧的 PTS 是否可能与流式传输时同一文件中同一帧的 PTS 不同?

当我使用 av_read_frame 读取帧时,我将视频流存储在 AVStream 中。使用 avcodec_decode_video2 解码帧后,我使用 av_frame_get_best_effort_timestamp 将该帧的时间戳存储在 int64_t 中。现在,如果程序从文件中获取输入,我会得到与将输入(来自同一文件)流式传输到程序时不同的时间戳。

要更改输入类型,我只需将 argv 参数从“/path/to/file.mp4”更改为“udp://localhost:1234”,然后在命令行中使用 ffmpeg 流式传输文件:“ ffmpeg -re -i /path/to/file.mp4 -f mpegts udp://localhost:1234"。会不会是因为“-f mpegts”参数改变了媒体的某些特性?

以下是我的代码(简化)。通过阅读 ffmpeg 邮件列表档案,我意识到我正在寻找的 time_base 在 AVStream 而不是 AVCodecContext 中。而不是使用 av_frame_get_best_effort_timestamp 我也尝试使用 packet.pts 但结果没有改变。 我需要时间戳来了解正在接收的流视频中的帧号。 我非常感谢任何形式的帮助。

//..
//argv[1]="/file.mp4";
argv[1]="udp://localhost:7777";
// define AVFormatContext, AVFrame, etc.
// register av, avcodec, avformat_network_init(), etc.
avformat_open_input(&pFormatCtx, argv, NULL, NULL);
avformat_find_stream_info(pFormatCtx, NULL);
// find the video stream...
// pointer to the codec context...
// open codec...
pFrame=av_frame_alloc();
while(av_read_frame(pFormatCtx, &packet)>=0) {
        AVStream *strem = pFormatCtx->streams[videoStream];
        if(packet.stream_index==videoStream) {
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
            if(frameFinished) {
                int64_t perts = av_frame_get_best_effort_timestamp(pFrame);
                if (isMyFrame(pFrame)){
                     cout << perts*av_q2d(strem->time_base) << "\n";
                }
             }
}
//free allocated space
}
//..

【问题讨论】:

    标签: c++ stream ffmpeg libavcodec libavformat


    【解决方案1】:

    时间戳存储在容器级别,因此更改容器可以更改时间戳。此外,TS 为每一帧存储一个时间戳(基于 90kHz 时钟)。 MP4 仅存储假定开始时间为 0 的帧持续时间(由于第一个 PTS 为零,第一个 DTS stream_base 单位返回你的滴答声。对于 TS 编解码器->stream_base 始终为 1/90000

    【讨论】:

    • 非常感谢,非常有用。在这种情况下,我假设流命令中的“-f mpegts”参数会即时更改容器,对吧?
    猜你喜欢
    • 2015-03-31
    • 2012-09-24
    • 2012-12-17
    • 2014-07-29
    • 2019-03-18
    • 2019-01-15
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多