程序步骤
1. 处理参数
1 | char* src; //输入数据 |
2. 配置输入上下文,打开多媒体文件
通过 avformat_open_input 打开输入的多媒体文件,并初始化输入上下文:
1 | if((ret = avformat_open_input(&pFmtCtx, src, NULL, NULL)) < 0){ |
3. 配置输出上下文
通过 avformat_alloc_output_context2 为目标文件创建输出上下文:
1 | avformat_alloc_output_context2(&oFmtCtx, NULL, NULL, dst); |
4. 打开输出文件
通过 avio_open2 打开目标文件,用于写入处理后的多媒体数据:
1 | ret = avio_open2(&oFmtCtx->pb, dst, AVIO_FLAG_WRITE, NULL, NULL); |
5. 在输出上下文创建流
遍历输入文件的所有流,根据需要创建对应的输出流,同时初始化 stream_map:
1 | stream_map = av_calloc(pFmtCtx->nb_streams, sizeof(int)); |
6. 写多媒体文件头到目的文件
1 | ret = avformat_write_header(oFmtCtx, NULL); |
7. 从源多媒体文件中读取数据到目的文件中
通过 av_read_frame 读取输入文件的帧数据,并通过 av_interleaved_write_frame 将处理后的数据写入目标文件:
1 | while(av_read_frame(pFmtCtx, &pkt) >= 0){ |
8. 写多媒体文件尾到文件中
1 | av_write_trailer(oFmtCtx); |
9. 将申请的资源释放掉
1 | __ERROR: |
说些什么吧!