ffmpeg dav转mp4 cpp示例
时间: 2023-10-09 14:06:47 浏览: 73
使用ffmpeg进行视频格式转换的简单例子.docx
以下是使用 FFmpeg C++ API 将 DAV 文件转换为 MP4 文件的示例代码:
```cpp
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <fstream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "Usage: " << argv[0] << " input.dav output.mp4" << endl;
return 1;
}
char* input_file = argv[1];
char* output_file = argv[2];
av_register_all();
avcodec_register_all();
AVFormatContext* input_format_ctx = NULL;
AVFormatContext* output_format_ctx = NULL;
if (avformat_open_input(&input_format_ctx, input_file, NULL, NULL) != 0) {
cout << "Error: could not open input file." << endl;
return 1;
}
if (avformat_find_stream_info(input_format_ctx, NULL) < 0) {
cout << "Error: could not find stream information." << endl;
return 1;
}
if (avformat_alloc_output_context2(&output_format_ctx, NULL, NULL, output_file) < 0) {
cout << "Error: could not allocate output context." << endl;
return 1;
}
AVCodec* codec = NULL;
AVCodecParameters* codec_params = NULL;
int video_stream_index = -1;
for (int i = 0; i < input_format_ctx->nb_streams; i++) {
AVStream* stream = input_format_ctx->streams[i];
codec_params = stream->codecpar;
codec = avcodec_find_decoder(codec_params->codec_id);
if (codec == NULL) {
continue;
}
if (codec_params->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
break;
}
}
if (video_stream_index == -1) {
cout << "Error: could not find video stream." << endl;
return 1;
}
AVStream* input_stream = input_format_ctx->streams[video_stream_index];
AVStream* output_stream = avformat_new_stream(output_format_ctx, codec);
if (output_stream == NULL) {
cout << "Error: could not create output stream." << endl;
return 1;
}
if (avcodec_parameters_copy(output_stream->codecpar, codec_params) < 0) {
cout << "Error: could not copy codec parameters." << endl;
return 1;
}
AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);
if (codec_ctx == NULL) {
cout << "Error: could not allocate codec context." << endl;
return 1;
}
if (avcodec_parameters_to_context(codec_ctx, codec_params) < 0) {
cout << "Error: could not convert codec parameters to codec context." << endl;
return 1;
}
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
cout << "Error: could not open codec." << endl;
return 1;
}
av_dump_format(input_format_ctx, 0, input_file, 0);
av_dump_format(output_format_ctx, 0, output_file, 1);
AVPacket packet;
int frame_cnt = 0;
if (avformat_write_header(output_format_ctx, NULL) < 0) {
cout << "Error: could not write output file header." << endl;
return 1;
}
while (av_read_frame(input_format_ctx, &packet) >= 0) {
if (packet.stream_index != video_stream_index) {
av_packet_unref(&packet);
continue;
}
AVFrame* frame = av_frame_alloc();
int ret = avcodec_send_packet(codec_ctx, &packet);
if (ret < 0) {
av_packet_unref(&packet);
av_frame_free(&frame);
continue;
}
while (ret >= 0) {
ret = avcodec_receive_frame(codec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
av_frame_free(&frame);
break;
}
sws_scale(sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P, 0, NULL, NULL, NULL), frame->data, frame->linesize, 0, codec_ctx->height, frame->data, frame->linesize);
frame_cnt++;
frame->pts = frame_cnt * input_stream->time_base.den / av_rescale_q(1, AV_TIME_BASE_Q, input_stream->time_base);
frame->pkt_dts = frame->pts;
av_packet_unref(&packet);
av_init_packet(&packet);
if (avcodec_send_frame(codec_ctx, frame) < 0) {
av_frame_free(&frame);
continue;
}
while (1) {
ret = avcodec_receive_packet(codec_ctx, &packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
av_packet_rescale_ts(&packet, codec_ctx->time_base, output_stream->time_base);
packet.stream_index = output_stream->index;
if (av_write_frame(output_format_ctx, &packet) < 0) {
av_packet_unref(&packet);
continue;
}
av_packet_unref(&packet);
}
av_frame_free(&frame);
}
}
if (av_write_trailer(output_format_ctx) < 0) {
cout << "Error: could not write output file trailer." << endl;
return 1;
}
avformat_close_input(&input_format_ctx);
avformat_free_context(input_format_ctx);
if (!(output_format_ctx->flags & AVFMT_NOFILE)) {
avio_closep(&output_format_ctx->pb);
}
avformat_free_context(output_format_ctx);
return 0;
}
```
其中,使用 `avformat_open_input` 打开输入文件, `avformat_find_stream_info` 查找流信息, `avformat_alloc_output_context2` 分配输出上下文, `avcodec_find_decoder` 查找解码器, `avcodec_open2` 打开解码器, `av_read_frame` 读取帧, `avcodec_send_packet` 发送数据包, `avcodec_receive_frame` 接收帧, `sws_scale` 进行图像缩放, `avcodec_send_frame` 发送帧, `avcodec_receive_packet` 接收数据包, `av_write_frame` 写入数据帧, `av_write_trailer` 写入尾帧, `avformat_close_input` 关闭输入文件, `avformat_free_context` 释放上下文等函数完成转换任务。
使用时,需要将 DAV 文件和 MP4 文件的路径传入程序作为参数,例如:
```bash
./dav2mp4 input.dav output.mp4
```
阅读全文