extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}
int main() {
av_register_all();
AVFormatContext* formatContext = avformat_alloc_context();
if (avformat_open_input(&formatContext, "4k.mp4", NULL, NULL) != 0) {
fprintf(stderr, "Error opening input file.\n");
return -1;
}
if (avformat_find_stream_info(formatContext, NULL) < 0) {
fprintf(stderr, "Error finding stream information.\n");
return -1;
}
av_dump_format(formatContext, 0, "4k.mp4", 0);
AVCodec* codec = NULL;
AVCodecContext* codecContext = NULL;
int videoStreamIndex = -1;
for (int i = 0; i < formatContext->nb_streams; i++) {
if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
codec = avcodec_find_decoder(formatContext->streams[i]->codecpar->codec_id);
codecContext = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codecContext, formatContext->streams[i]->codecpar);
avcodec_open2(codecContext, codec, NULL);
break;
}
}
AVPacket packet;
av_init_packet(&packet);
AVFrame* frame = av_frame_alloc();
AVFrame* frameRGB = av_frame_alloc();
struct SwsContext* swsContext = sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt, codecContext->width, codecContext->height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL);
while (av_read_frame(formatContext, &packet) >= 0) {
if (packet.stream_index == videoStreamIndex) {
int response = avcodec_send_packet(codecContext, &packet);
if (response < 0) {
fprintf(stderr, "Error decoding frame.\n");
return -1;
}
response = avcodec_receive_frame(codecContext, frame);
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
continue;
}
else if (response < 0) {
fprintf(stderr, "Error decoding frame.\n");
return -1;
}
av_image_alloc(frameRGB->data, frameRGB->linesize, codecContext->width, codecContext->height, AV_PIX_FMT_YUV420P, 1);
sws_scale(swsContext, (uint8_t const* const*)frame->data, frame->linesize, 0, codecContext->height, frameRGB->data, frameRGB->linesize);
// Save frame as YUV file
FILE* yuvFile = fopen("4k.yuv", "wb");
if (yuvFile) {
fwrite(frameRGB->data[0], 1, codecContext->width * codecContext->height, yuvFile); // Y
fwrite(frameRGB->data[1], 1, codecContext->width * codecContext->height / 4, yuvFile); // U
fwrite(frameRGB->data[2], 1, codecContext->width * codecContext->height / 4, yuvFile); // V
fclose(yuvFile);
}
av_frame_unref(frame);
av_frame_unref(frameRGB);
}
av_packet_unref(&packet);
}
av_frame_free(&frame);
av_frame_free(&frameRGB);
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
avformat_free_context(formatContext);
sws_freeContext(swsContext);
return 0;
}
ffmpeg教程笔记(C++ffmpeg库应用开发)命令行使用篇——第四章:Fmpeg转码——ffmpeg软编解码(软件编解码、Software Codec)
2024-05-26 00:20:29 56 阅读