思路说明:
1、经过两天在网上在搜索学习,让我一个FFmpeg小白完成了YUV转TS的过程。
2、主要思路是:yuv->.h264->mp4->ts
3、至于为什么需要经过那么多转换才能得到TS。我目前也无法解释的清楚,我只知道yuv无法直接转到ts必须通过中间的编译(.H264)才行,具体的请看雷神教学。本人也照着学习而已。
4、至于为什么不能从(.H264直接转TS)应该我尝试了很多次都失败就换个蠢的办法,如有大神有成功案例请不要吝啬贡献出来。
步骤:
准备工作:
1、给自己的IDE配置FFmpeg环境,我用的是VS2017.网上教程很多,自己百度。
2、准备好需要转换的YUV文件(src01_hrc00_576x324_cut_30_2M.yuv)
3、yuv转.H264代码
#include "stdafx.h"
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif
int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index) {
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
AV_CODEC_CAP_DELAY))
return 0;
while (1) {
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = avcodec_encode_video2(fmt_ctx->streams[stream_index]->codec, &enc_pkt,
NULL, &got_frame);
av_frame_free(NULL);
if (ret < 0)<