将视频的视频流提取出来, 并保存为.h264文件

一个视频文件(如.mp4)里面包含了视频流以及音频流
其中视频的主流编码是H.264, 音频的主流编码是AAC

  • 本文提供一种在C++中使用ffmpeg来提取一个视频文件的视频流的方法, 输出为.h264文件, 可用于RTP传输。不过要求视频本身就是使用H.264编码的。并且电脑已经配置好ffmpeg的环境变量。
#include <cstdlib> // For system function

int main() {
    const char* inputFileName = "input_file.mp4";
    const char* outputFileName = "output_file.h264";

    // Construct FFmpeg command to extract video stream
    std::string ffmpegCommand = "ffmpeg -i ";
    ffmpegCommand += inputFileName;
    ffmpegCommand += " -c:v copy -an -bsf:v h264_mp4toannexb -f h264 ";
    ffmpegCommand += outputFileName;

    // Execute FFmpeg command
    int status = system(ffmpegCommand.c_str());
    if (status != 0) {
        fprintf(stderr, "Error: FFmpeg command failed.\n");
        return 1;
    }

    return 0;
}



更多推荐