ffmpeg MP4转h264 cpp代码
时间: 2023-10-05 22:11:08 浏览: 139
以下是使用FFmpeg将MP4视频转换为H.264视频的C++代码示例:
```
#include <iostream>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
}
using namespace std;
int main(int argc, char* argv[]) {
AVFormatContext* inputFormatContext = nullptr;
AVCodecContext* inputCodecContext = nullptr;
AVCodec* inputCodec = nullptr;
AVPacket inputPacket;
AVFrame* inputFrame = nullptr;
AVFormatContext* outputFormatContext = nullptr;
AVCodecContext* outputCodecContext = nullptr;
AVCodec* outputCodec = nullptr;
AVPacket outputPacket;
AVFrame* outputFrame = nullptr;
const char* inputFilePath = "input.mp4";
const char* outputFilePath = "output.h264";
// Open input file
if (avformat_open_input(&inputFormatContext, inputFilePath, nullptr, nullptr) != 0) {
cerr << "Failed to open input file: " << inputFilePath << endl;
return -1;
}
// Retrieve stream information
if (avformat_find_stream_info(inputFormatContext, nullptr) < 0) {
cerr << "Failed to retrieve input stream information" << endl;
return -1;
}
// Find video stream index
int videoStreamIndex = -1;
for (int i = 0; i < inputFormatContext->nb_streams; i++) {
if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) {
cerr << "Failed to find video stream" << endl;
return -1;
}
// Get video codec parameters
AVCodecParameters* inputCodecParameters = inputFormatContext->streams[videoStreamIndex]->codecpar;
// Find video codec
inputCodec = avcodec_find_decoder(inputCodecParameters->codec_id);
if (inputCodec == nullptr) {
cerr << "Failed to find input video codec" << endl;
return -1;
}
// Allocate input codec context
inputCodecContext = avcodec_alloc_context3(inputCodec);
if (inputCodecContext == nullptr) {
cerr << "Failed to allocate input codec context" << endl;
return -1;
}
// Copy codec parameters to input codec context
if (avcodec_parameters_to_context(inputCodecContext, inputCodecParameters) < 0) {
cerr << "Failed to copy input codec parameters to context" << endl;
return -1;
}
// Open input codec
if (avcodec_open2(inputCodecContext, inputCodec, nullptr) < 0) {
cerr << "Failed to open input codec" << endl;
return -1;
}
// Allocate input frame
inputFrame = av_frame_alloc();
if (inputFrame == nullptr) {
cerr << "Failed to allocate input frame" << endl;
return -1;
}
// Open output file
if (avformat_alloc_output_context2(&outputFormatContext, nullptr, nullptr, outputFilePath) < 0) {
cerr << "Failed to allocate output format context" << endl;
return -1;
}
// Find output video codec
outputCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (outputCodec == nullptr) {
cerr << "Failed to find output video codec" << endl;
return -1;
}
// Allocate output codec context
outputCodecContext = avcodec_alloc_context3(outputCodec);
if (outputCodecContext == nullptr) {
cerr << "Failed to allocate output codec context" << endl;
return -1;
}
// Set output codec parameters
outputCodecContext->bit_rate = inputCodecContext->bit_rate;
outputCodecContext->width = inputCodecContext->width;
outputCodecContext->height = inputCodecContext->height;
outputCodecContext->time_base = inputCodecContext->time_base;
outputCodecContext->framerate = inputCodecContext->framerate;
outputCodecContext->gop_size = inputCodecContext->gop_size;
outputCodecContext->max_b_frames = inputCodecContext->max_b_frames;
outputCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
// Open output codec
if (avcodec_open2(outputCodecContext, outputCodec, nullptr) < 0) {
cerr << "Failed to open output codec" << endl;
return -1;
}
// Allocate output frame
outputFrame = av_frame_alloc();
if (outputFrame == nullptr) {
cerr << "Failed to allocate output frame" << endl;
return -1;
}
outputFrame->format = outputCodecContext->pix_fmt;
outputFrame->width = outputCodecContext->width;
outputFrame->height = outputCodecContext->height;
if (av_frame_get_buffer(outputFrame, 0) < 0) {
cerr << "Failed to allocate output frame buffer" << endl;
return -1;
}
// Write output file header
if (avformat_write_header(outputFormatContext, nullptr) < 0) {
cerr << "Failed to write output file header" << endl;
return -1;
}
// Read input file frames
while (av_read_frame(inputFormatContext, &inputPacket) == 0) {
if (inputPacket.stream_index == videoStreamIndex) {
// Decode input packet
if (avcodec_send_packet(inputCodecContext, &inputPacket) < 0) {
cerr << "Failed to send input packet for decoding" << endl;
return -1;
}
while (avcodec_receive_frame(inputCodecContext, inputFrame) == 0) {
// Convert input frame to output frame
if (sws_scale(
sws_getContext(inputCodecContext->width, inputCodecContext->height, inputCodecContext->pix_fmt,
outputCodecContext->width, outputCodecContext->height, outputCodecContext->pix_fmt,
SWS_BILINEAR, nullptr, nullptr, nullptr),
inputFrame->data, inputFrame->linesize, 0, inputCodecContext->height,
outputFrame->data, outputFrame->linesize) <= 0) {
cerr << "Failed to convert input frame to output frame" << endl;
return -1;
}
// Encode output frame
if (avcodec_send_frame(outputCodecContext, outputFrame) < 0) {
cerr << "Failed to send output frame for encoding" << endl;
return -1;
}
while (avcodec_receive_packet(outputCodecContext, &outputPacket) == 0) {
// Write output packet to file
if (av_write_frame(outputFormatContext, &outputPacket) < 0) {
cerr << "Failed to write output packet to file" << endl;
return -1;
}
av_packet_unref(&outputPacket);
}
}
}
av_packet_unref(&inputPacket);
}
// Flush output codec
if (avcodec_send_frame(outputCodecContext, nullptr) < 0) {
cerr << "Failed to send NULL frame for flushing output codec" << endl;
return -1;
}
while (avcodec_receive_packet(outputCodecContext, &outputPacket) == 0) {
// Write output packet to file
if (av_write_frame(outputFormatContext, &outputPacket) < 0) {
cerr << "Failed to write output packet to file" << endl;
return -1;
}
av_packet_unref(&outputPacket);
}
// Write output file trailer
if (av_write_trailer(outputFormatContext) < 0) {
cerr << "Failed to write output file trailer" << endl;
return -1;
}
// Clean up
av_frame_free(&inputFrame);
avcodec_free_context(&inputCodecContext);
avformat_close_input(&inputFormatContext);
av_frame_free(&outputFrame);
avcodec_free_context(&outputCodecContext);
avformat_free_context(outputFormatContext);
return 0;
}
```
此示例假设输入文件为H.264编码,输出文件为H.264编码。如果输入文件为其他编码格式,则可能需要更改示例中的某些参数。此外,使用FFmpeg时需要包含相应的库文件和头文件。
阅读全文