FFmpeg是一种跨平台的开源多媒体处理工具,它提供了丰富的功能和命令,包括图像和视频的处理、编码、解码等。下面是使用FFmpeg函数对图像进行比例(宽高比)处理的方法:
下面是一些示例代码,用于演示如何使用FFmpeg函数对图像进行比例(宽高比)处理:
#include <stdio.h>
#include <stdlib.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
int main(int argc, char* argv[]) {
AVFormatContext* formatCtx = NULL;
AVCodecContext* codecCtx = NULL;
AVCodec* codec = NULL;
AVFrame* frame = NULL;
AVPacket packet;
struct SwsContext* swsCtx = NULL;
if (argc < 4) {
fprintf(stderr, "Usage: %s <input_file> <output_file> <new_width>:<new_height>\n", argv[0]);
return -1;
}
const char* inputFile = argv[1];
const char* outputFile = argv[2];
int newWidth = atoi(strtok(argv[3], ":"));
int newHeight = atoi(strtok(NULL, ":"));
av_register_all();
if (avformat_open_input(&formatCtx, inputFile, NULL, NULL) != 0) {
fprintf(stderr, "Failed to open input file\n");
return -1;
}
if (avformat_find_stream_info(formatCtx, NULL) < 0) {
fprintf(stderr, "Failed to find stream information\n");
return -1;
}
int videoStreamIndex = -1;
for (int i = 0; i < formatCtx->nb_streams; i++) {
if (formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) {
fprintf(stderr, "Failed to find video stream\n");
return -1;
}
codecCtx = formatCtx->streams[videoStreamIndex]->codec;
codec = avcodec_find_decoder(codecCtx->codec_id);
if (codec == NULL) {
fprintf(stderr, "Failed to find decoder\n");
return -1;
}
if (avcodec_open2(codecCtx, codec, NULL) < 0) {
fprintf(stderr, "Failed to open decoder\n");
return -1;
}
frame = av_frame_alloc();
swsCtx = sws_getContext(codecCtx->width, codecCtx->height, codecCtx->pix_fmt,
newWidth, newHeight, AV_PIX_FMT_RGB24,
SWS_BILINEAR, NULL, NULL, NULL);
FILE* outputFilePtr = fopen(outputFile, "wb");
if (outputFilePtr == NULL) {
fprintf(stderr, "Failed to open output file\n");
return -1;
}
while (av_read_frame(formatCtx, &packet) >= 0) {
if (packet.stream_index == videoStreamIndex) {
avcodec_decode_video2(codecCtx, frame, &frameFinished, &packet);
if (frameFinished) {
uint8_t* data[1];
data[0] = (uint8_t*)malloc(newWidth * newHeight * 3);
if (data[0] != NULL) {
int linesize[1] = { newWidth * 3 };
sws_scale(swsCtx, frame->data, frame->linesize, 0, codecCtx->height, data, linesize);
fwrite(data[0], 1, newWidth * newHeight * 3, outputFilePtr);
free(data[0]);
}
}
}
av_free_packet(&packet);
}
fclose(outputFilePtr);
av_frame_free(&frame);
avcodec_close(codecCtx);
avformat_close_input(&formatCtx);
return 0;
}
请注意,上述代码仅提供了基本的示例,具体的应用场景和优化方法可能因实际需求而异。此外,推荐的腾讯云相关产品和产品介绍链接地址请参考腾讯云官方文档和网站,以获取最新的信息和资源。