解码成的yuv文件用ffply测试是可以播放的,但是pcm文件播放时全是杂音,不知道是哪一步错误了,但是看ffmpeg官方demo,找不出具体问题,知道的麻烦评论区留言指点下,谢谢!!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale_internal.h>
#include <libswscale/swscale.h>int main(int argc, char **argv)
{const char *inFilename;/* 必须置空,不然avformat_open_input中不会去分配内存,然后导致段错误 */AVFormatContext* inputFormatCtx = NULL; int ret;AVPacket* packet;AVFrame*  frame;SwsContext* img_convert_ctx = NULL;if (argc <= 1) {fprintf(stderr, "Usage: %s <input file>\n", argv[0]);exit(0);}inFilename  = argv[1];/*** 调用avformat_alloc_context分配初始化inputFormatCtx,通过输入的url判断协议/文件类型//再通过对应类型的URLProtocol结构体open和read   ****/ret = avformat_open_input(&inputFormatCtx,inFilename,0,0);if(ret < 0){fprintf(stderr, "Could not open input file '%s'", inFilename);goto end;}/* 假设输入文件中有音频和视频,则会给对应的AVStream结构体赋值,查找对应流的对应解码器 */ret = avformat_find_stream_info(inputFormatCtx,0);if(ret != 0){fprintf(stderr, "Failed to retrieve input stream information");goto end;}/* 找出输入文件中视频和音频流位置 */int audioIndex = av_find_best_stream(inputFormatCtx,AVMEDIA_TYPE_AUDIO,-1,-1,NULL,0);int videoIndex = av_find_best_stream(inputFormatCtx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0);/* 再根据位置获取对应的流 */AVStream* audioStream = inputFormatCtx->streams[audioIndex];AVStream* videoStream = inputFormatCtx->streams[videoIndex];/* 获取对应流类型的解码器 */AVCodec* audioCodec = avcodec_find_decoder(audioStream->codecpar->codec_id);AVCodec* videoCodec = avcodec_find_decoder(videoStream->codecpar->codec_id);/* 根据解码器分配打开编解码器上下文 */AVCodecContext* audioCodecCtx = avcodec_alloc_context3(audioCodec);avcodec_parameters_to_context(audioCodecCtx,audioStream->codecpar);avcodec_open2(audioCodecCtx,audioCodec,0);  AVCodecContext* videoCodecCtx = avcodec_alloc_context3(videoCodec);avcodec_parameters_to_context(videoCodecCtx,videoStream->codecpar);avcodec_open2(videoCodecCtx,videoCodec,0);/* 打印输入文件的流信息 */av_dump_format(inputFormatCtx, 0, inFilename, 0);packet = av_packet_alloc();frame = av_frame_alloc();/* 初始化一个用来转换数据的帧  */AVFrame* tempFrame = av_frame_alloc();tempFrame->width = videoCodecCtx->width;tempFrame->height = videoCodecCtx->height;tempFrame->format = AV_PIX_FMT_YUV420P;/* 调用此函数前需社会好宽高和格式,不然无法判断是视频 ;下面函数会初始化lineSize,指每一行占多少字节,按照对齐的方式来计算的,大于等于width */av_frame_get_buffer(tempFrame,4);FILE* outYUV420File = fopen("source/outFile.yuv","wb+");FILE* outPcmFile = fopen("source/outFile.pcm","wb+");int videoDataLen = videoCodecCtx->width * videoCodecCtx->height;/* 读取数据,帧的倍数数据  */while (av_read_frame(inputFormatCtx,packet) >= 0){/*  刚开始以为这里读取的包数据就是裸流数据了,写下来无法播放,看二进制数据不是标准的h264,开头还有些协议数据*/if(packet->stream_index == videoIndex){ret = avcodec_send_packet(videoCodecCtx,packet);while(ret >= 0){if((ret = avcodec_receive_frame(videoCodecCtx,frame))>=0){/* 根据输入的参数分配初始化SwsContext */img_convert_ctx = sws_getContext(frame->width, frame->height, videoCodecCtx->pix_fmt,videoCodecCtx->width, videoCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);/* 实际转换函数,第四个参数指第一列出来的位置,填0表示从头处理 */sws_scale(img_convert_ctx, frame->data, frame->linesize, 0, videoCodecCtx->height,tempFrame->data, tempFrame->linesize);//sws_freeContext(img_convert_ctx);/* 把yuv数据写到文件 */fwrite(tempFrame->data[0],1,videoDataLen,outYUV420File);fwrite(tempFrame->data[1],1,videoDataLen/4,outYUV420File);fwrite(tempFrame->data[2],1,videoDataLen/4,outYUV420File);}}}else if(packet->stream_index == audioIndex){ret = avcodec_send_packet(audioCodecCtx,packet);while(ret >= 0){ret =avcodec_receive_frame(audioCodecCtx,frame);int audioLen = av_get_bytes_per_sample(audioCodecCtx->sample_fmt);for(int i = 0;i< frame->nb_samples;++i)for(int j = 0; j < audioCodecCtx->channels;++j)fwrite(frame->data[j] + audioLen*i,1,audioLen,outPcmFile);}}}end:avformat_close_input(&inputFormatCtx);if(outYUV420File)fclose(outYUV420File);if(outPcmFile)fclose(outPcmFile);avcodec_free_context(&videoCodecCtx);avcodec_free_context(&audioCodecCtx);av_packet_free(&packet);av_frame_free(&tempFrame);av_frame_free(&frame);return 0;
}

参考链接:https://blog.csdn.net/zhucehenfanren/article/details/105363443?ops_request_misc=&request_id=&biz_id=102&utm_term=ffmpeg%204.2&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-105363443.pc_search_all_es&spm=1018.2226.3001.4187

ffmpeg4.2.2 实现封装格式文件(如flv)解码成yuv420p和pcm相关推荐

  1. 模拟输入H.264流,输出封装格式文件(API版)

    每次从H.264文件读入一定数据量的数据,模拟输入H.264流,最终输出封装格式文件. //H264ToContainer_Win32.h extern "C" {//@param ...

  2. 利用FFmpeg将H.264文件读入内存,再输出封装格式文件

    /***先将H.264文件读入内存,*再输出封装格式文件.*/ #include "stdafx.h"#define __STDC_CONSTANT_MACROSextern &q ...

  3. 模拟输入H.264流,输出封装格式文件

    /***每次从H.264文件读取IO_BUFFER_SIZE字节的数据,*模拟输入H.264流,最终输出封装格式文件.*/ #include "stdafx.h"#define _ ...

  4. 最简单的基于FFMPEG的封装格式转换器(无编解码)

    2019独角兽企业重金招聘Python工程师标准>>> 本文介绍一个基于FFMPEG的封装格式转换器.所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(对应. ...

  5. 最简单的基于FFMPEG 4.2的封装格式转换器(无编解码MP4转FLV)

    文章目录 最简单的基于FFMPEG 4.2的封装格式转换器(无编解码) 配置 代码 结果 关键函数说明 avformat_open_input avformat_find_stream_info av ...

  6. 成功解决:将后缀.pyx格式文件(linux环境)编译成pyd文件(windows环境下)实现python编程加载或导入

    成功解决:将后缀.pyx格式文件(linux环境)编译成pyd文件(windows环境下)实现python编程加载或导入 目录 解决问题 解决思路 解决方法 解决问题 .pyx格式文件,在window ...

  7. 将base64格式的字体信息解码成可用的字体文件

    将base64格式的字体信息解码成可用的字体文件 爬取文字显示乱码,查询之后发现没有这个字体文字,属于自定义字体 解决方法: <script>!function(w,d){if(!w.Ac ...

  8. xv格式文件提取flv

    xv格式视频文件时迅雷看看播放器使用的特殊视频的缓冲文件,如果使用迅雷看看播放器播放需要联网状态.这样也是为了保护视频的版权. 下面是使用xv视频提取器从xv视频文件中提取出flv格式的视频文件. 1 ...

  9. 音视频封装格式转换器(支持avi格式转换),基于FFmpeg4.1实现(音视频学习笔记二)

    之前参照雷霄骅博士的最简单的基于FFMPEG的封装格式转换器(无编解码)的博客和FFmpeg官网的example,实现一个简单的封装格式转换器.但是后来我发现我想从mp4格式转换成avi格式的时候会报 ...

最新文章

  1. 新转机!2020年想裸辞的程序员们注意了
  2. ClassLoader引发的类型转换异常(转载)
  3. 维秘天使超模美女Romee Strijd性感运动照片
  4. 将h.264视频流封装成flv格式文件
  5. ABAP DOI详解(3)
  6. INTERSPEECH 2020 AutoSpeech论文征集挑战赛正式启动
  7. idea离线下载lombok,以及lobok版本不兼容
  8. 合法C标识符(信息学奥赛一本通-T1134)
  9. nodejs 前端 返回数组给_互联网寒冬,一年经验字节跳动、虾皮、快手、拼多多前端面试总结...
  10. fishhook源码分析
  11. android,PHP解析JSON数组,JSONArray,JSONOject,乱码
  12. k8s中各组件和kube apiserver通信时的认证和鉴权
  13. 谷歌浏览器Chrome播放rtsp视频流解决方案
  14. Redis数据类型及编码
  15. 电脑两个,电脑有两个系统盘怎么办
  16. C++FTP客户端库:ftplibpp的使用
  17. jQuery 和React
  18. 小鑫の日常系列故事(十)——排名次
  19. 如何只用与非门、或非门构成或门、与门、非门?
  20. 阿里云大数据——搭建企业级数据分析平台

热门文章

  1. 在投票系统方法的原则刷票(突破ip限制刷票PHP版)
  2. 编译原理三大经典:龙书 虎书 鲸书
  3. JAVA快速排序递归算法
  4. 中央机关及其直属机构2007年考试录用公务员行政职业能力测试
  5. adum1201参考电路_【VIP专享】数字式隔离器ADUM1201在RS232总线通信系统中的应用
  6. [DataAnalysis]数据分析基础-茆诗松数理统计
  7. git将本地文件推到远程仓库
  8. 又在开会……[原文写作时间:2006年6月26日]
  9. NodeJS中的UDP通信
  10. 江苏省普通话水平测试计算机评分细则,江苏省普通话水平测试评分细则(试行)...