ffmpeg系列打开ffmpeg格式转换和缩放器
来源:互联网 发布:手机相片日期软件 编辑:程序博客网 时间:2024/06/05 20:10
main.cpp代码如下:
#include "myplayer.h"#include <QtWidgets/QApplication>#pragma comment(lib,"avformat.lib")#pragma comment(lib,"avutil.lib")#pragma comment(lib,"avcodec.lib")#pragma comment(lib,"swscale.lib")extern "C"{ #include <libavformat/avformat.h> #include <libswscale/swscale.h>}static double r2d(AVRational r){return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;}int main(int argc, char *argv[]){av_register_all();char *path = "test.mp4";AVFormatContext *ac = NULL;int re = avformat_open_input(&ac, path, 0, 0);if (re != 0){//打开文件失败char buf[1024] = { 0 };av_strerror(re, buf, sizeof(buf));printf("file %s open failed because of : %s", path, buf);getchar();return -1;}int totalSec = ac->duration / AV_TIME_BASE;printf("指定的视频文件有 %d分%d秒\n", totalSec / 60, totalSec % 60);int videoStream = 0;AVCodecContext *videoCtx = NULL;SwsContext *cCtx = NULL;int outWidth = 640;int outHeight = 480;for (int i = 0; i < ac->nb_streams; i++){AVCodecContext *enc = ac->streams[i]->codec;if (enc->codec_type == AVMEDIA_TYPE_VIDEO){videoStream = i;videoCtx = enc;AVCodec *codec = avcodec_find_decoder(enc->codec_id);if (!codec){printf("无法解码此视频文件\n");return -1;}int err = avcodec_open2(enc, codec, NULL);if (err != 0){char buf[1024] = { 0 };av_strerror(err, buf, sizeof(buf));printf(buf);return -2;}printf("\n");printf("成功打开视频编码流\n");}}AVFrame *yuv = av_frame_alloc();for (;;){AVPacket pkt;re = av_read_frame(ac, &pkt);if (re != 0){break;}if (pkt.stream_index != videoStream){av_packet_unref(&pkt);continue;}int pts = pkt.pts * r2d(ac->streams[pkt.stream_index]->time_base) * 1000;//得到帧的毫秒值//=============================================================================int re = avcodec_send_packet(videoCtx,&pkt);if (re != 0){av_packet_unref(&pkt);continue;}re = avcodec_receive_frame(videoCtx,yuv);if (re != 0){av_packet_unref(&pkt);continue;}printf("[D]___");//=============================================================================//打开ffmpeg格式转换和缩放器cCtx = sws_getCachedContext(cCtx, videoCtx->width, videoCtx->height, videoCtx->pix_fmt,outWidth, outHeight, AV_PIX_FMT_BGRA, SWS_BICUBIC,NULL, NULL, NULL);if (cCtx){printf("sws_getCachedContext success\n");}else{printf("sws_getCachedContext failed\n");}printf("pts = %d 毫秒\n", pts);av_packet_unref(&pkt);}if (cCtx){sws_freeContext(cCtx);cCtx = NULL;}avformat_close_input(&ac);ac = NULL;QApplication a(argc, argv);MyPlayer w;w.show();return a.exec();}
运行结果:
阅读全文
0 0
- ffmpeg系列打开ffmpeg格式转换和缩放器
- ffmpeg缩放和格式转换yuv数据
- ffmpeg 格式转换过程
- ffmpeg 格式转换过程
- FFMPEG 声音格式转换
- ffmpeg 转换语音格式
- ffmpeg系列:使用ffmpeg转换为RGB数据并缩放视频
- ffmpeg系列:使用C++类封装ffmpeg,封装视频帧缩放转换rgb功能ToRGB()方法
- ffmpeg系列之打开ffmpeg视频解码器
- ffmpeg 支持aac格式转换
- Linux下ffmpeg格式转换
- ffmpeg 支持aac格式转换
- FFMPEG封装格式转换(remuxer)
- ffmpeg图片格式转换缩放的问题sws_scale
- FFMPEG 图像拉伸缩放及数据格式转换
- FFMPEG 图像拉伸缩放及数据格式转换
- 使用FFmpeg和mencoder将视频转换为flv格式
- ffmpeg命令行实现YUV和RGB之间格式的转换
- 深入浅出Signals综合分析
- Android Studio手动配置Gradle的方法
- pip install pysftp出现的错误
- 查看ip地址是否被占用
- 我的学习记录36
- ffmpeg系列打开ffmpeg格式转换和缩放器
- 理解 ES6 Generator 函数
- ..\SYSTEM\usart\usart.c(41): error: #260-D: explicit type is missing ("int" assumed)
- 前端js 常用积累
- 12.2、拦截器配置和执行顺序
- jQuery 选择器
- 深度学习之基础模型-SEP-Nets
- Python 随笔1
- aliyun ubuntu install jdk