h264 裸流打包成mp4 注意事项
来源:互联网 发布:软件招聘信息网 编辑:程序博客网 时间:2024/05/21 17:31
需求: Android 端把网络摄像头的一段正在播放的视频流,截取保存成mp4(按录像按钮时开始录像)。
实现: ffmpeg + x264 + sdl;
h264 裸流 打包成MP4,在网上也有一大堆文章,ffmpeg 也有一个muxing 的 example,大致流程都是一样的,参考ffmpeg的muxing.c 就可以写一个。我这里把我在这个过程中遇到的问题写出来,这些问题困扰了我很久才解决,谁叫我是视频方面的小白呢。
这三个问题其实很简单,但如果对这方面不了解的话,耗掉的时间还是很多的。
1,dts,pts:
在write_frame()之前,每一个avpacket 数据都要设置 dts,pts,因为我的视频没有B帧,所以dts = pts 便可;pts 一开始我也不知道要怎么设, 参考muxing.c,设置成写入的帧数量便可(结合问题3);
2,sps,pps;
我接受到的裸流里面 sps +pps + i帧 是放在一个NALU 里面的,在保存成mp4时这个sps 非常重要,我一开始没有设置,打包后的Mp4 普通的播放器就不能识别;
sps 在创建 编码器时候传递给编码器; sps 是 00 00 00 01 后面的,不包含这个00 00 00 01 这个码;pps 我还没用到。
- case AVMEDIA_TYPE_VIDEO:
- c->codec_id = codec_id;
- LOGE("add_stream AVMEDIA_TYPE_VIDEO c->time_base.num = %d",ost->st->codec->time_base.num);
- c->bit_rate = 400000;
- /* Resolution must be a multiple of two. */
- c->width = 352;
- c->height = 288;
- /* timebase: This is the fundamental unit of time (in seconds) in terms
- * of which frame timestamps are represented. For fixed-fps content,
- * timebase should be 1/framerate and timestamp increments should be
- * identical to 1. */
- ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE};//STREAM_FRAME_RATE };
- c->time_base =ost->st->time_base;
- c->gop_size = 12; /* emit one intra frame every twelve frames at most */
- c->pix_fmt = STREAM_PIX_FMT;
- if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
- /* just for testing, we also add B frames */
- c->max_b_frames = 2;
- }
- if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
- /* Needed to avoid using macroblocks in which some coeffs overflow.
- * This does not happen with normal video, it just happens here as
- * the motion of the chroma plane does not match the luma plane. */
- c->mb_decision = 2;
- }
- c->extradata = spsinfo;
- c->extradata_size = 10;
- break;
3,fps:
在保存成mp4后,调试过程中各种fps都出来了,最后仔细看muxing.c ,发现其实要设置正确很简单:在pts 按1递增后,用一个很简单的函数就解决了这个问题。
c->time_base : 输入的帧率fps = 25 :{1,25},ost->st->time_base : 输出的帧率; 我这里设置和输入的一样就好了。
- packet->pts = (ptsInc++);;// * (90000/STREAM_FRAME_RATE);
- packet->dts = packet->pts;
- // packet->duration = 8/1000000;
- // packet->pos = -1;//ptsInc;
- av_packet_rescale_ts(packet, c->time_base, ost->st->time_base);
- h264 裸流打包成mp4 注意事项
- h264 裸流打包成mp4 注意事项
- Mp4v2实现h264+aac打包成Mp4视频文件
- Mp4v2实现h264+aac打包成Mp4视频文件
- Mp4v2实现h264+aac打包成Mp4视频文件
- 多媒体 Mp4v2实现h264+aac打包成Mp4视频文件
- Mp4v2实现h264+aac打包成Mp4视频文件
- Mp4v2实现h264+aac打包成Mp4视频文件
- Mp4v2实现h264+aac打包成Mp4视频文件
- H264码流封装MP4
- 利用ffmpeg0.6.1把.h264纯码流打包成.mp4 .avi等格式
- H264编码 封装成MP4格式 视频流 RTP封包
- H264编码 封装成MP4格式 视频流 RTP封包
- H264编码 封装成MP4格式 视频流 RTP封包
- H264编码 封装成MP4格式 视频流 RTP封包
- H264编码 封装成MP4格式 视频流 RTP封包
- H264编码 封装成MP4格式 视频流 RTP封包
- H264编码 封装成MP4格式 视频流 RTP封包
- Tomcat6的项目迁移到weblogic11g时报错
- 查看Oracle执行计划的几种方法
- opengl透明mask图片做蒙版效果的实现
- UVa--10048 Audiophobia(floyd)
- openwrt 下基于按键法的WPS功能实现
- h264 裸流打包成mp4 注意事项
- 如何通过maven官网查询相关依赖的具体代码和版本?
- nodejs篇(一)
- 兼容浏览器的js事件绑定函数
- 【杭电oj】1068 - Girls and Boys(最大独立集数,匈牙利算法)
- HDU 2836 Traversal(线段树+离散化+DP)
- docx的解析
- lightoj 1079 - Just another Robbery 01背包
- 160329集合