IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++用ffmpeg提取视频转码并另存 -> 正文阅读

[C++知识库]C++用ffmpeg提取视频转码并另存

//有些大段的备注是机器翻译,知道大概意思就行。

//C++,用ffmpeg提取视频转码并另存 ,本例为验证程序,只有这个程序搞懂了,才可能用ffmpge做个播放器什么的。

#include <QCoreApplication>
#include <iostream>
using namespace std;
extern "C"{
? ? #include <libavcodec/avcodec.h>
? ? #include <libavformat/avformat.h>
}


int main(int argc, char *argv[])
{
? ? QCoreApplication a(argc, argv);

? ? //const char* url {"a.mkv"};
? ? const char* url {"b.mp4"};
? ? const char* out {"out.yuv"};
? ? AVFormatContext *fmt_ctx {};
? ? int re = avformat_open_input(
? ? ? ? ? ? ? ? &fmt_ctx, ?//AVFormatContext
? ? ? ? ? ? ? ? url,
? ? ? ? ? ? ? ? 0, ?// 0表示自动选择解封器
? ? ? ? ? ? ? ? 0 //参数设置,比如rtsp的延时时间
? ? ? ? ? ? ? ? );
? ? if(re<0)
? ? {
? ? ? ? cout<<"avformat_open_input err"<<endl;
? ? ? ? exit(-1);
? ? }

? ? av_dump_format(fmt_ctx, 0, url, 0);//打印文件信息

? ? re = avformat_find_stream_info(fmt_ctx, 0);//获取流信息
? ? if(re<0)
? ? {
? ? ? ? cout<<"avformat_find_stream_info err"<<endl;
? ? ? ? exit(-1);
? ? }

? ? re=av_find_best_stream(fmt_ctx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0);//在文件中找到“最佳”流。最佳流是根据各种启发式来确定的,因为最有可能是用户所期望的。如果decoder参数非null, av_find_best_stream将为流的编解码器找到默认的解码器;无法找到解码器的流将被忽略。
? ? if(re<0)
? ? {
? ? ? ? cout<<"av_find_best_stream err"<<endl;
? ? ? ? exit(-1);
? ? }

? ? int stream_index=re;
? ? AVStream *video_stream=fmt_ctx->streams[stream_index];
? ? const AVCodec *av_dec=avcodec_find_decoder(video_stream->codecpar->codec_id);//查找具有匹配编解码器ID的注册解码器。

? ? AVCodecContext *dec_ctx=avcodec_alloc_context3(av_dec);//分配一个AVCodecContext并设置它的字段为默认值。结果结构应该使用avcodec_free_context()释放。

? ? re=avcodec_parameters_to_context(dec_ctx,video_stream->codecpar); //根据提供的编解码器参数的值填充编解码器上下文。在编解码器中,任何在“par”中具有相应字段的已分配字段将被释放并替换为par中相应字段的副本。编解码器中没有对应字段的字段不会被修改。
? ? if(re<0)
? ? {
? ? ? ? cout<<"avcodec_parameters_to_context err"<<endl;
? ? ? ? exit(-1);
? ? }

? ? re=avcodec_open2(dec_ctx,av_dec,nullptr);
? ? if(re<0)
? ? {
? ? ? ? cout<<"avcodec_open2 err"<<endl;
? ? ? ? exit(-1);
? ? }


? ? FILE *output_video_file=fopen(out,"wb");
? ? if(!output_video_file)
? ? {
? ? ? ? cout<<"fopen(out,wb) err"<<endl;
? ? ? ? exit(-1);
? ? }

? ? av_dump_format(fmt_ctx,0,url,0); ?//打印文件信息,和上面的已有所不同
? ? video_stream=fmt_ctx->streams[stream_index];
? ? cout<<"video_stream="<<video_stream<<endl;


? ? AVPacket *pkt= av_packet_alloc();

? ? AVFrame *frame= av_frame_alloc();

? ? while(av_read_frame(fmt_ctx,pkt)>=0) ?//返回流的下一帧。这个函数返回文件中存储的内容,并且不验证解码器是否存在有效的帧。它将文件中存储的内容分割成帧,并为每个调用返回一个帧。它不会省略有效帧之间的无效数据,以便给解码器解码的最大可能信息。如果成功,返回的数据包是引用计数的(设置了pkt->buf)并且无限期有效。当不再需要该包时,必须使用av_packet_unref()释放它。对于视频,数据包只包含一帧。对于音频,如果每一帧都有一个已知的固定大小(例如PCM或ADPCM数据),它包含一个整数帧数。如果音频帧有一个可变的大小(例如MPEG音频),那么它包含一个帧。pkt->pts, pkt->dts和pkt->持续时间总是设置为AVStream中的正确值。Time_base单位(猜测格式是否不能提供它们)。pkt->pts可以是AV_NOPTS_VALUE如果视频格式有b帧,所以最好依赖于pkt->dts如果你不解压有效负载。
? ? {
? ? ? ? int re {0};
? ? ? ? if(pkt->stream_index==stream_index)
? ? ? ? {
? ? ? ? ? ? re=avcodec_send_packet(dec_ctx,pkt);
? ? ? ? ? ? if(re<0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cout<<"avcodec_send_packet err"<<endl;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? while(1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? re=avcodec_receive_frame(dec_ctx,frame);//一次avcodec_send_packet可能需要多次avcodec_receive_frame
? ? ? ? ? ? ? ? if(re<0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? cout<<"avcodec_receive_frame err"<<endl;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?//write_frame_to_yuv(frame);以下为写入文件函数
? ? ? ? ? ? ? ? uint8_t **pBuf=frame->data;
? ? ? ? ? ? ? ? int *pStride=frame->linesize;
? ? ? ? ? ? ? ? for(size_t i=0;i<3;i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int32_t width=(i==0?frame->width:frame->width/2);
? ? ? ? ? ? ? ? ? ? int32_t height=(i==0?frame->height:frame->height/2);
? ? ? ? ? ? ? ? ? ? for(size_t j=0;j<height;j++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? fwrite(pBuf[i],1,width,output_video_file);
? ? ? ? ? ? ? ? ? ? ? ? pBuf[i]+=pStride[i];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? cout<<"frame="<<frame<<endl;
? ? ? ? ? ? ? //write_frame_to_yuv(frame);以上为写入文件函数
? ? ? ? ? ? }

? ? ? ? }

? ? ? ? av_packet_unref(pkt);

? ? }

//以下为分配空间释放
? ? av_frame_free(&frame);
? ? av_packet_free(&pkt);
? ? avcodec_free_context(&dec_ctx);
? ? avformat_close_input(&fmt_ctx);
? ? if (output_video_file != nullptr) {
? ? ? ? fclose(output_video_file);
? ? ? ? output_video_file = nullptr;
? ? ? ? cout<<"output_video_file close"<<endl;

? ? }

end:
? ? return a.exec();

}
/*
?生成的out.yuv文件用以下命令测试,要注意视频文件的分辨率,分辨率不同需要调整,例如 1920x800、640x320等
ffplay -f rawvideo -video_size 1920x800 out.yuv
*/
?

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-07-05 23:22:06  更:2022-07-05 23:23:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 17:31:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码