FFMPEG在Visual Studio中的配置

打开https://ffmpeg.zeranoe.com/builds/,该网站中的FFMPEG分为3个版本:Static,Shared,Dev。开发时需要下载后两个版本,分别部署在C:\ffmpeg-shared  和  C:\ffmpeg-dev  。

Visual Studio中的部署如下:

工程名右键->属性->配置属性->c/c++->常规->附加包含目录 

C:\ffmpeg-dev\include

工程名右键 ->属性->配置属性->链接器->常规  附加库目录

C:\ffmpeg-dev\lib

工程名右键 ->属性->配置属性-> 链接器 -> 输入 -> 附加依赖项

avcodec.lib;

avformat.lib;

avutil.lib;

avdevice.lib;

avfilter.lib;

postproc.lib;

swresample.lib;

swscale.lib;

工程名右键 ->属性->配置属性->调试->环境  

           PATH=C:\ffmpeg-shared\bin;

 

首先,你需要下载并安装 FFmpeg。然后,在 Visual Studio 创建一个新的 C++ 项目。 接下来,你需要在项目的属性配置 FFmpeg 的路径和链接库。在项目属性选择“VC++目录”并添加 FFmpeg 的头文件路径和库文件路径。然后在“链接器”选项卡添加链接库,包括 avcodec.lib、avformat.lib、avutil.lib、swscale.lib 和 swresample.lib。 在代码,你需要使用 FFmpeg 的 API 来打开 MP4 文件,提取视频流,并将其转换为 H264 格式。以下是一个简单的示例代码: ```c++ #include <iostream> #include <string> #include <fstream> #include <vector> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> } int main(int argc, char* argv[]) { // Initialize FFmpeg av_register_all(); avcodec_register_all(); // Open input file AVFormatContext* inputContext = nullptr; if (avformat_open_input(&inputContext, argv[1], nullptr, nullptr) != 0) { std::cerr << "Failed to open input file." << std::endl; return -1; } // Find video stream int videoStreamIndex = -1; AVCodecParameters* videoCodecParameters = nullptr; for (unsigned int i = 0; i < inputContext->nb_streams; i++) { AVCodecParameters* codecParameters = inputContext->streams[i]->codecpar; if (codecParameters->codec_type == AVMEDIA_TYPE_VIDEO) { videoCodecParameters = codecParameters; videoStreamIndex = i; break; } } if (videoStreamIndex == -1 || videoCodecParameters == nullptr) { std::cerr << "Failed to find video stream." << std::endl; return -1; } // Find video codec AVCodec* videoCodec = avcodec_find_decoder(videoCodecParameters->codec_id); if (videoCodec == nullptr) { std::cerr << "Failed to find video codec." << std::endl; return -1; } // Open video codec AVCodecContext* videoCodecContext = avcodec_alloc_context3(videoCodec); if (avcodec_parameters_to_context(videoCodecContext, videoCodecParameters) != 0) { std::cerr << "Failed to copy codec parameters to codec context." << std::endl; return -1; } if (avcodec_open2(videoCodecContext, videoCodec, nullptr) != 0) { std::cerr << "Failed to open video codec." << std::endl; return -1; } // Create output file std::ofstream outputFile("output.h264", std::ios::binary); if (!outputFile) { std::cerr << "Failed to create output file." << std::endl; return -1; } // Allocate frame and packet AVFrame* frame = av_frame_alloc(); AVPacket* packet = av_packet_alloc(); // Initialize scaler SwsContext* scalerContext = sws_getContext( videoCodecContext->width, videoCodecContext->height, videoCodecContext->pix_fmt, videoCodecContext->width, videoCodecContext->height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, nullptr, nullptr, nullptr); if (scalerContext == nullptr) { std::cerr << "Failed to initialize scaler." << std::endl; return -1; } // Read frames and convert while (av_read_frame(inputContext, packet) == 0) { if (packet->stream_index == videoStreamIndex) { // Decode packet if (avcodec_send_packet(videoCodecContext, packet) != 0) { std::cerr << "Failed to send packet to decoder." << std::endl; return -1; } while (avcodec_receive_frame(videoCodecContext, frame) == 0) { // Convert to YUV420P AVFrame* scaledFrame = av_frame_alloc(); scaledFrame->format = AV_PIX_FMT_YUV420P; scaledFrame->width = videoCodecContext->width; scaledFrame->height = videoCodecContext->height; av_frame_get_buffer(scaledFrame, 32); sws_scale(scalerContext, frame->data, frame->linesize, 0, videoCodecContext->height, scaledFrame->data, scaledFrame->linesize); // Write frame to file std::vector<uint8_t> outputBuffer(scaledFrame->width * scaledFrame->height * 3 / 2); uint8_t* outputData = outputBuffer.data(); int outputSize = av_image_copy_to_buffer( outputData, scaledFrame->linesize, scaledFrame->data, scaledFrame->linesize, AV_PIX_FMT_YUV420P, scaledFrame->width, scaledFrame->height, 1); outputFile.write(reinterpret_cast<char*>(outputData), outputSize); av_frame_unref(scaledFrame); } } av_packet_unref(packet); } // Cleanup av_frame_free(&frame); av_packet_free(&packet); sws_freeContext(scalerContext); avcodec_free_context(&videoCodecContext); avformat_close_input(&inputContext); outputFile.close(); return 0; } ``` 在运行时,你需要将 MP4 文件的路径作为命令行参数传递给程序。程序将将转换后的 H264 数据写入名为“output.h264”的文件。 注意:这只是一个简单的示例,可能并不适用于所有的 MP4 文件。你需要根据你的需求进行修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值