FFmpeg实现h264 转mpeg1video 存储

本文介绍了如何使用FFmpeg将h264文件解码后编码成mpeg1video格式。由于最新版ffmpeg 4.2.2不支持mpeg1video编码,建议使用3.2.14版本。提供的代码示例经过测试,生成的mpg文件可正常播放,强调参照官方sample进行开发能提高效率。
摘要由CSDN通过智能技术生成

需要实现的功能: 把h264文件进行解码,解码后然后进行mpeg1的编码. 这一步完成接下来再实现存储为ts,本篇代码实现了第一部分,

注意事项:
ffmpeg版本问题,今天2020.2.11的最新版本是ffmpeg 4.2.2,这个对mpeg1video的编码支持不行.

使用ffmpeg sample encode_video.c会报:

mpeg1video The encoder timebase is not set的错误.

其实这个导致的主要问题是版本问题,推荐大家使用:ffmpeg 3.2.14版本.

具体代码:

#ifdef __cplusplus
extern "C" {
   
#endif
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/time.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <libavutil/hwcontext.h>
#include <libavutil/error.h>
#include <libavfilter/avfilter.h>
#ifdef __cplusplus
}
#endif

/**
 * 功能:    实现把h264转码为mpeg1的格式s
 * 参考:    https://www.jianshu.com/p/8cd90dba49e1
 */

int main()
{
   
    av_register_all();
    avcodec_register_all();
    avfilter_register_all();
    avformat_network_init();

    const char *fname = "/mnt/d/x264_encode.264";

    // 从h264文件中提取出一帧一帧的数据
    AVFormatContext *inputCtx = NULL;
    // open the input file
	if (avformat_open_input(&inputCtx,fname,NULL,NULL) != 0)
	{
   
		fprintf(stderr, "avformat open input file :%s fail.\n" , fname);
		return -1;
	}
    AVPacket packet;
    int ret = 0;
 //   int framesize = 0;

    // h624解码为yuv
    AVCodec *codec_h264 = avcodec_find_decoder(AV_CODEC_ID_H264);
    AVCodecContext *ctx_h264 = avcodec_alloc_context3(codec_h264);
    if (avcodec_open2(ctx_h264,codec_h264,NULL) < 0)
    {
   
        /* code */
        fprintf(stderr, "avcodec_open2 h264 fail.\n" );
        exit(1
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值