使用ffmpeg将mp4转为ts(代码实现)

使用ffmpeg将mp4转为ts的命令格式如下
ffmpeg - i b.mp4 -codec copy -bsf h264_mp4toannexb a.ts

如果不使用-bsf h264_mp4toannexb参数 会提示错误
主要是因为使用了mp4中的h264编码 而h264有两种封装:
一种是annexb模式 传统模式 有statrtcode SPS和PPS是在ES中 另一种是mp4模式 一般mp4 mkv avi会没有startcode SPS和PPS以及其他信息被封装在container中 每一个frame前面是这个frame的长度 很多解码器只支持annexb这种模式 因此需要将mp4做转换 在ffmpeg中用h264_mp4toannexb_filter可以做转换 所以需要使用-bsf h264_mp4toannexb来进行转换

代码实现主要分为三个步骤:
1.初始化filter av_bitstream_filter_init
2.使用filter转换av_bitstream_filter_filter
3.关闭filter av_bitstream_filter_close

其他与正常的转封装相同

#include <stdio.h>
#include <stdlib.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#define  FORMAT "mpegts"
#define  OUTPUT_FILE  "./fuck.ts"
#define  INPUT_FILE  "./a.mp4"
int main(int argc ,char *argv[])
{
	AVPacket pkt;//压缩帧
	AVFormatContext *input_fmtctx=NULL;//封装格式上下文
	AVFormatContext *output_fmtctx=NULL;//输出视频封装格式上下文
	AVCodecContext *enc_ctx=NULL;
	AVCodevContext *dec_ctx=NULL;//编解码上下文
	AVCodec *encoder=NULL;//每种编解码器对应一个该结构体
	AVBitStreamFilterContext *vbsf=NULL;
	int ret=0;
	int i=0;
	int have_key=0;
	static int first_pkt=1;
    av_register_all();
    if(avformat_open_input(&input_fmtctx,INPUT_FILE,NULL,NULL)<0){
      av_log(NULL,AV_LOG_ERROR,"Cannot open the file %s\n","./a.mp4");
      return  -ENOENT;
    } 
	

}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值