音视频开发五:visual studio集成使用FFmpeg

1. 下载ffmpeg编译好的Windows版本

ffmpeg 官网 -> download -> 选择Windows系统 -> 选择gyan.dev版本-> shared版本

在这里插入图片描述

在Windows系统上,Gyan.dev和BtbN都提供了FFmpeg的预编译版本。Gyan.dev通常使用MSVC编译器,而BtbN使用MinGW编译器。因此,Gyan.dev的版本可能会更符合Windows标准,而BtbN的版本可能会更加开放和跨平台。

选择 shared版本

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m66NA3E5-1680012961479)(D:\Users\11154214\AppData\Roaming\Typora\typora-user-images\image-20230313223028435.png)]

各个版本的区别介绍

  • essential就是简版,只包含ffmpeg.exe、ffplay.exe、ffprobe.exe
  • Full版适用于终端用户,因为它包含了所有的可执行文件和静态库,用户可以从命令行调用FFmpeg的工具来进行视频处理
  • Full-Shared版仅包含共享库和工具,不包含可执行文件和静态库,这使得开发者可以使用FFmpeg的功能实现自己的应用程序或集成FFmpeg到自己的项目中。

2. 设置环境变量

下载后解压,文件目录如下:

在这里插入图片描述

设置一下将bin目录设置到环境变量中去。配置环境变量不明白自行搜索。

接下来在打开命令终端
输入 ffmpeg -version。

在这里插入图片描述

这样就表示成功。

3. visual studio2022 配置FFmpeg环境

1. 设置附加包含目录

首先右键点击创建的项目FFmpegTest,选择属性

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lcPAQxdS-1680012961481)(D:\Users\11154214\AppData\Roaming\Typora\typora-user-images\image-20230314093940736.png)]

在配置属性中选择C/C++目录中常规属性的附加包含目录添加ffumpeg的头文件目录。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vLqnHGFv-1680012961483)(D:\Users\11154214\AppData\Roaming\Typora\typora-user-images\image-20230314094324989.png)]

2. 设置附加库目录

在配置属性中选择链接器中常规属性附加库目录添加ffmpeg的lib路径。

在这里插入图片描述

3.设置附加依赖项

在链接器输入属性中附加依赖项添加具体的依赖库名称

在这里插入图片描述

avcodec.lib
avdevice.lib
avfilter.lib
avformat.lib
avutil.lib
postproc.lib
swresample.lib
swscale.lib

4. 配置调试环境

上述环境基本就设置好了,如果在使用ffmpeg库编译代码的时候,提示没有找到xx.dll库。

原因: 由于没有找到ffmpeg动态库所在的目录。

解决: 设置调试时的环境变量,把ffmpeg动态库的路径添加到环境变量中。

配置属性-> 调试 -> 环境- >添加路径

在这里插入图片描述

4. 测试是否配置成功

1. 编写代码

#include <iostream>

extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}

int main()
{
    std::cout << "Hello World!\n";
    printf("%s\n", avcodec_configuration());


}

使用extern “C” 。"extern C"是一种语言特性,用于在C++代码中引用C语言函数。因为ffmpeg是用C语言编写的,告诉链接器在链接的时候用C函数规范来链接,确保C++代码和FFmpeg使用相同的函数名(主要原因是C++和C程序编译完成后在目标代码中命名规则不同)。这样,当我们在编译和链接时使用C++编译器时,编译器将关闭函数名称修饰,从而让我们能够直接使用FFmpeg的C函数

2.执行结果

在这里插入图片描述

这样就配置成功了。

  • 11
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用FFmpeg进行dav转mp4的cpp代码(在Visual Studio环境下): ``` extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/avutil.h> #include <libavutil/dict.h> #include <libavutil/error.h> #include <libavutil/mathematics.h> #include <libavutil/opt.h> #include <libavutil/samplefmt.h> #include <libswscale/swscale.h> } int main() { const char* input_file_path = "input.dav"; const char* output_file_path = "output.mp4"; // Open input file AVFormatContext* format_ctx = nullptr; if (avformat_open_input(&format_ctx, input_file_path, nullptr, nullptr) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not open input file %s\n", input_file_path); return -1; } // Get stream info if (avformat_find_stream_info(format_ctx, nullptr) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not find stream information\n"); return -1; } // Find video and audio stream AVCodec* video_codec = nullptr; AVCodec* audio_codec = nullptr; int video_stream_index = -1; int audio_stream_index = -1; for (int i = 0; i < format_ctx->nb_streams; ++i) { if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; video_codec = avcodec_find_decoder(format_ctx->streams[i]->codecpar->codec_id); } else if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { audio_stream_index = i; audio_codec = avcodec_find_decoder(format_ctx->streams[i]->codecpar->codec_id); } } if (video_stream_index == -1 && audio_stream_index == -1) { av_log(nullptr, AV_LOG_ERROR, "Could not find any video or audio stream\n"); return -1; } // Open video codec AVCodecContext* video_codec_ctx = nullptr; if (video_codec != nullptr) { video_codec_ctx = avcodec_alloc_context3(video_codec); if (avcodec_parameters_to_context(video_codec_ctx, format_ctx->streams[video_stream_index]->codecpar) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not copy video codec parameters to context\n"); return -1; } if (avcodec_open2(video_codec_ctx, video_codec, nullptr) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not open video codec\n"); return -1; } } // Open audio codec AVCodecContext* audio_codec_ctx = nullptr; if (audio_codec != nullptr) { audio_codec_ctx = avcodec_alloc_context3(audio_codec); if (avcodec_parameters_to_context(audio_codec_ctx, format_ctx->streams[audio_stream_index]->codecpar) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not copy audio codec parameters to context\n"); return -1; } if (avcodec_open2(audio_codec_ctx, audio_codec, nullptr) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not open audio codec\n"); return -1; } } // Open output file AVFormatContext* output_format_ctx = nullptr; if (avformat_alloc_output_context2(&output_format_ctx, nullptr, nullptr, output_file_path) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not create output context\n"); return -1; } // Add video stream to output file AVStream* video_stream = nullptr; if (video_codec_ctx != nullptr) { video_stream = avformat_new_stream(output_format_ctx, video_codec); if (video_stream == nullptr) { av_log(nullptr, AV_LOG_ERROR, "Could not create video stream\n"); return -1; } if (avcodec_parameters_copy(video_stream->codecpar, format_ctx->streams[video_stream_index]->codecpar) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not copy video codec parameters to output stream\n"); return -1; } if (avcodec_parameters_to_context(video_stream->codec, video_stream->codecpar) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not copy video codec parameters to output stream context\n"); return -1; } video_stream->codec->codec_tag = 0; video_stream->time_base = format_ctx->streams[video_stream_index]->time_base; } // Add audio stream to output file AVStream* audio_stream = nullptr; if (audio_codec_ctx != nullptr) { audio_stream = avformat_new_stream(output_format_ctx, audio_codec); if (audio_stream == nullptr) { av_log(nullptr, AV_LOG_ERROR, "Could not create audio stream\n"); return -1; } if (avcodec_parameters_copy(audio_stream->codecpar, format_ctx->streams[audio_stream_index]->codecpar) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not copy audio codec parameters to output stream\n"); return -1; } if (avcodec_parameters_to_context(audio_stream->codec, audio_stream->codecpar) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not copy audio codec parameters to output stream context\n"); return -1; } audio_stream->codec->codec_tag = 0; audio_stream->time_base = format_ctx->streams[audio_stream_index]->time_base; } // Open output file for writing if (!(output_format_ctx->oformat->flags & AVFMT_NOFILE)) { if (avio_open(&output_format_ctx->pb, output_file_path, AVIO_FLAG_WRITE) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not open output file %s\n", output_file_path); return -1; } } // Write header to output file if (avformat_write_header(output_format_ctx, nullptr) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not write header to output file\n"); return -1; } // Convert video frames AVFrame* video_frame = av_frame_alloc(); AVFrame* video_frame_rgb = av_frame_alloc(); if (video_codec_ctx != nullptr) { SwsContext* sws_ctx = sws_getContext(video_codec_ctx->width, video_codec_ctx->height, video_codec_ctx->pix_fmt, video_codec_ctx->width, video_codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr); if (sws_ctx == nullptr) { av_log(nullptr, AV_LOG_ERROR, "Could not create SwsContext\n"); return -1; } av_image_alloc(video_frame_rgb->data, video_frame_rgb->linesize, video_codec_ctx->width, video_codec_ctx->height, AV_PIX_FMT_RGB24, 1); AVPacket packet; av_init_packet(&packet); while (av_read_frame(format_ctx, &packet) >= 0) { if (packet.stream_index == video_stream_index) { if (avcodec_send_packet(video_codec_ctx, &packet) == 0) { while (avcodec_receive_frame(video_codec_ctx, video_frame) == 0) { sws_scale(sws_ctx, video_frame->data, video_frame->linesize, 0, video_codec_ctx->height, video_frame_rgb->data, video_frame_rgb->linesize); video_frame_rgb->pts = video_frame->pts; if (avcodec_send_frame(video_stream->codec, video_frame_rgb) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not send video frame to output stream\n"); return -1; } while (avcodec_receive_packet(video_stream->codec, &packet) == 0) { if (av_write_frame(output_format_ctx, &packet) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not write video packet to output file\n"); return -1; } av_packet_unref(&packet); } } } } av_packet_unref(&packet); } avcodec_send_frame(video_stream->codec, nullptr); while (avcodec_receive_packet(video_stream->codec, &packet) == 0) { if (av_write_frame(output_format_ctx, &packet) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not write video packet to output file\n"); return -1; } av_packet_unref(&packet); } sws_freeContext(sws_ctx); } // Convert audio frames AVFrame* audio_frame = av_frame_alloc(); if (audio_codec_ctx != nullptr) { AVPacket packet; av_init_packet(&packet); while (av_read_frame(format_ctx, &packet) >= 0) { if (packet.stream_index == audio_stream_index) { if (avcodec_send_packet(audio_codec_ctx, &packet) == 0) { while (avcodec_receive_frame(audio_codec_ctx, audio_frame) == 0) { audio_frame->pts = audio_frame->best_effort_timestamp; if (avcodec_send_frame(audio_stream->codec, audio_frame) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not send audio frame to output stream\n"); return -1; } while (avcodec_receive_packet(audio_stream->codec, &packet) == 0) { if (av_write_frame(output_format_ctx, &packet) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not write audio packet to output file\n"); return -1; } av_packet_unref(&packet); } } } } av_packet_unref(&packet); } avcodec_send_frame(audio_stream->codec, nullptr); while (avcodec_receive_packet(audio_stream->codec, &packet) == 0) { if (av_write_frame(output_format_ctx, &packet) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not write audio packet to output file\n"); return -1; } av_packet_unref(&packet); } } // Write trailer to output file if (av_write_trailer(output_format_ctx) < 0) { av_log(nullptr, AV_LOG_ERROR, "Could not write trailer to output file\n"); return -1; } // Close output file if (!(output_format_ctx->oformat->flags & AVFMT_NOFILE)) { avio_close(output_format_ctx->pb); } // Free resources avcodec_free_context(&video_codec_ctx); avcodec_free_context(&audio_codec_ctx); avformat_close_input(&format_ctx); avformat_free_context(output_format_ctx); av_frame_free(&video_frame); av_frame_free(&video_frame_rgb); av_frame_free(&audio_frame); return 0; } ``` 这段代码使用FFmpeg的多个库来进行视频和音频的解码、转换和编码,具体实现细节可以参考FFmpeg的官方文档。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值