C/C++编程:ffmpeg学习(windows + vs2019 + cmake + vcpkg)

1060 篇文章 297 订阅

准备

vcpkg安装ffmpeg

1、vcpkg:win10安装使用 vcpkg

2、安装 ffmpeg

vcpkg install ffmpeg:x64-windows

vs中创建一个工程

在这里插入图片描述
在这里插入图片描述

使用

1、cmakelist.txt


cmake_minimum_required (VERSION 3.8)

 
include_directories("C:/Users/oceanstar/vcpkg/win/vcpkg/installed/x64-windows/include")
link_directories("C:/Users/oceanstar/vcpkg/win/vcpkg/installed/x64-windows/lib")



add_executable (cmake_test "cmake_test.cpp" "cmake_test.h")



target_link_libraries(
        cmake_test
        avfilter
        avformat
        avcodec
        avutil
        swresample
        swscale)



代码

#include <stdio.h> 
#include <Windows.h>
#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavformat/avformat.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif

int main()
{
	printf("%s\n", avcodec_configuration());
	return 0;
}

效果:
在这里插入图片描述

参考:

Visual Studio 开发(二):VS 2017配置FFmpeg开发环境

为了使用FFmpe修复H265视频流,需要进行以下步骤: 1. 包含FFmpeg头文件和链接到库。可以使用以下命令链接FFmpeg库: ```shell g++ -o output input.cpp -lavformat -lavcodec -lavutil ``` 2. 打开输入文件并获取流信息: ```c++ AVFormatContext *inputFormatContext = NULL; if (avformat_open_input(&inputFormatContext, inputFileName, NULL, NULL) < 0) { // 打开输入文件失败 return; } if (avformat_find_stream_info(inputFormatContext, NULL) < 0) { // 获取流信息失败 return; } ``` 3. 查找视频流并打开解码器: ```c++ 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) { // 没有找到视频流 return; } AVCodecParameters *videoCodecParameters = inputFormatContext->streams[videoStreamIndex]->codecpar; AVCodec *videoCodec = avcodec_find_decoder(videoCodecParameters->codec_id); if (videoCodec == NULL) { // 找不到解码器 return; } AVCodecContext *videoCodecContext = avcodec_alloc_context3(videoCodec); if (avcodec_parameters_to_context(videoCodecContext, videoCodecParameters) < 0) { // 复制解码器参数失败 return; } if (avcodec_open2(videoCodecContext, videoCodec, NULL) < 0) { // 打开解码器失败 return; } ``` 4. 创建输出文件并写入头信息: ```c++ AVFormatContext *outputFormatContext = NULL; if (avformat_alloc_output_context2(&outputFormatContext, NULL, NULL, outputFileName) < 0) { // 创建输出文件失败 return; } if (avio_open(&outputFormatContext->pb, outputFileName, AVIO_FLAG_WRITE) < 0) { // 打开输出文件失败 return; } AVStream *outputVideoStream = avformat_new_stream(outputFormatContext, NULL); if (outputVideoStream == NULL) { // 创建输出视频流失败 return; } AVCodecParameters *outputVideoCodecParameters = avcodec_parameters_alloc(); if (avcodec_parameters_copy(outputVideoCodecParameters, videoCodecParameters) < 0) { // 复制解码器参数失败 return; } outputVideoCodecParameters->codec_tag = 0; if (avformat_write_header(outputFormatContext, NULL) < 0) { // 写入头信息失败 return; } ``` 5. 读取视频帧并进行修复: ```c++ AVPacket packet; while (av_read_frame(inputFormatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { AVFrame *frame = av_frame_alloc(); if (frame == NULL) { // 分配帧内存失败 break; } if (avcodec_send_packet(videoCodecContext, &packet) < 0) { // 发送数据包到解码器失败 av_frame_free(&frame); break; } while (avcodec_receive_frame(videoCodecContext, frame) >= 0) { // 修复视频帧 // ... // 将修复后的帧写入输出文件 if (avcodec_send_frame(outputVideoCodecContext, frame) < 0) { // 发送帧到编码器失败 av_frame_free(&frame); break; } while (avcodec_receive_packet(outputVideoCodecContext, &packet) >= 0) { av_packet_rescale_ts(&packet, videoCodecContext->time_base, outputVideoStream->time_base); packet.stream_index = outputVideoStream->index; if (av_interleaved_write_frame(outputFormatContext, &packet) < 0) { // 写入数据包到输出文件失败 av_packet_unref(&packet); av_frame_free(&frame); break; } av_packet_unref(&packet); } } av_frame_free(&frame); } av_packet_unref(&packet); } ``` 6. 写入输出文件的尾信息并释放资源: ```c++ av_write_trailer(outputFormatContext); avcodec_free_context(&videoCodecContext); avformat_close_input(&inputFormatContext); avformat_free_context(inputFormatContext); avcodec_parameters_free(&outputVideoCodecParameters); avformat_free_context(outputFormatContext); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值