ffmpeg crop api使用
时间: 2023-11-23 07:57:59 浏览: 82
要使用FFmpeg的crop API,可以使用以下命令:
```
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
```
其中,w和h是输出视频的宽度和高度,x和y是要裁剪的左上角坐标。例如,如果要将输入视频裁剪为宽度为640,高度为360,左上角坐标为100,100,则可以使用以下命令:
```
ffmpeg -i input.mp4 -filter:v "crop=640:360:100:100" output.mp4
```
如果要在代码中使用crop API,可以使用AVFilterGraph和AVFilterContext结构体。以下是一个使用crop API的示例代码:
```c
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
AVFilter *buffersrc;
AVFilter *buffersink;
AVFilterInOut *outputs;
AVFilterInOut *inputs;
char args[512];
int ret;
/* 初始化filter graph */
filter_graph = avfilter_graph_alloc();
if (!filter_graph) {
return AVERROR(ENOMEM);
}
/* 初始化buffersource filter */
buffersrc = avfilter_get_by_name("buffer");
if (!buffersrc) {
return AVERROR_UNKNOWN;
}
/* 初始化buffersink filter */
buffersink = avfilter_get_by_name("buffersink");
if (!buffersink) {
return AVERROR_UNKNOWN;
}
/* 设置filter graph的inputs和outputs */
outputs = avfilter_inout_alloc();
inputs = avfilter_inout_alloc();
sprintf(args, "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
width, height, pix_fmt, time_base.num, time_base.den, pixel_aspect.num, pixel_aspect.den);
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", args, NULL, filter_graph);
if (ret < 0) {
return ret;
}
ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", NULL, NULL, filter_graph);
if (ret < 0) {
return ret;
}
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx = 0;
outputs->next = NULL;
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx;
inputs->pad_idx = 0;
inputs->next = NULL;
if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec, &inputs, &outputs, NULL)) < 0) {
return ret;
}
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) {
return ret;
}
/* 添加crop filter */
AVFilter *crop_filter = avfilter_get_by_name("crop");
AVFilterContext *crop_ctx;
char crop_args[256];
sprintf(crop_args, "%d:%d:%d:%d", out_width, out_height, x, y);
ret = avfilter_graph_create_filter(&crop_ctx, crop_filter, "crop", crop_args, NULL, filter_graph);
if (ret < 0) {
return ret;
}
if ((ret = avfilter_link(buffersrc_ctx, 0, crop_ctx, 0)) < 0) {
return ret;
}
if ((ret = avfilter_link(crop_ctx, 0, buffersink_ctx, 0)) < 0) {
return ret;
}
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) {
return ret;
}
/* 处理frame */
AVFrame *frame;
AVFrame *filt_frame;
int got_frame;
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
if (pkt.stream_index == video_stream_index) {
ret = avcodec_decode_video2(codec_ctx, frame, &got_frame, &pkt);
if (ret < 0) {
return ret;
}
if (got_frame) {
/* 处理frame */
ret = av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF);
if (ret < 0) {
return ret;
}
while (1) {
ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
if (ret < 0) {
return ret;
}
/* 处理filt_frame */
av_frame_unref(filt_frame);
}
}
}
av_packet_unref(&pkt);
}
/* 清理 */
avfilter_graph_free(&filter_graph);
av_frame_free(&frame);
av_frame_free(&filt_frame);
```
阅读全文