ffmpeg 编码程序
时间: 2023-08-25 07:14:25 浏览: 65
FFmpeg是一个开源的跨平台音视频编解码库和工具集,可用于录制、转换和流式传输音视频文件。以下是一个简单的FFmpeg编码程序示例,将输入文件转换为H.264格式的输出文件:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
}
int main(int argc, char** argv) {
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameRGB;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer;
if (argc < 3) {
printf("Usage: %s <input file> <output file>\n", argv[0]);
return -1;
}
av_register_all();
if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) {
printf("Could not open input file.\n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
printf("Could not find stream information.\n");
return -1;
}
av_dump_format(pFormatCtx, 0, argv[1], 0);
int videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
}
if (videoStream == -1) {
printf("Could not find video stream.\n");
return -1;
}
pCodecCtx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar);
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
printf("Unsupported codec.\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
printf("Could not open codec.\n");
return -1;
}
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
if (pFrame == NULL || pFrameRGB == NULL) {
printf("Could not allocate frames.\n");
return -1;
}
numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
if (buffer == NULL) {
printf("Could not allocate buffer.\n");
return -1;
}
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
int frameCount = 0;
FILE *fp = fopen(argv[2], "wb");
if (fp == NULL) {
printf("Could not open output file.\n");
return -1;
}
struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
avcodec_send_packet(pCodecCtx, &packet);
while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
int y_size = pCodecCtx->width * pCodecCtx->height;
fwrite(pFrameRGB->data[0], 1, y_size * 3, fp);
frameCount++;
}
av_packet_unref(&packet);
}
}
sws_freeContext(sws_ctx);
fclose(fp);
av_frame_free(&pFrame);
av_frame_free(&pFrameRGB);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
printf("Converted %d frames.\n", frameCount);
return 0;
}
```
以上示例代码仅供参考,并不能保证完全可用和正确。如果您需要使用FFmpeg进行音视频编解码,请仔细阅读官方文档,并参考更多示例代码。
阅读全文