转换使用libav MP4,TS

编程入门 行业动态 更新时间:2024-10-22 18:35:43
本文介绍了转换使用libav MP4,TS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想转换成MP4文件使用到MPEGTS libav 。的code工作,但输出不能由任何播放机上播放的文件。

I'm trying to convert an mp4 file to mpegts using libav. The code works, but outputs a file that can not be played by any player.

下面是输入文件(这只是视频,无音频)

Here is the input file (it is video only, no audio)

这里是code我到目前为止有:

here is the code I have so far:

int convert(const char *input, const char *output) { AVFormatContext *inputContext = NULL; AVFormatContext *outputContext = NULL; AVCodecContext *input_codec_context = NULL; AVCodecContext *output_codec_context = NULL; AVCodec *output_codec = NULL; AVCodec *input_codec = NULL; AVStream *stream = NULL; AVIOContext *avioContext = NULL; av_register_all(); avformat_network_init(); avformat_alloc_output_context2(&outputContext, NULL, "mpegts", output); avio_open(&avioContext, output, AVIO_FLAG_WRITE); outputContext->pb = avioContext; stream = avformat_new_stream(outputContext, output_codec); avformat_open_input(&inputContext, input, NULL, NULL); input_codec_context = inputContext->streams[0]->codec; input_codec = avcodec_find_decoder(inputContext->streams[0]->codec->codec_id); avcodec_open2(inputContext->streams[0]->codec, input_codec, NULL); avformat_find_stream_info(inputContext, NULL); outputContext->oformat = av_guess_format(NULL, output, NULL); output_codec = input_codec; output_codec_context = avcodec_alloc_context3(NULL); avcodec_copy_context(output_codec_context, input_codec_context); avcodec_open2(output_codec_context, output_codec, NULL); AVPacket packet; av_init_packet(&packet); avformat_write_header(outputContext, NULL); while (!av_read_frame(inputContext, &packet)) { av_write_frame(outputContext, &packet); } av_free_packet(&packet); av_write_trailer(outputContext); avformat_close_input(&inputContext); avformat_free_context(inputContext); avformat_free_context(outputContext); return 0; }

任何帮助AP preciated。

any help appreciated.

推荐答案

MP4 和 MPEGTS 承担不同的比特流格式H264。

mp4 and mpegts assume a different bitstream format for h264.

您需要插入 h264_mp4toannexb 来格式化你得到AVPacket。

You need to insert h264_mp4toannexb to reformat the AVPacket you are getting.

要分配上下文

AVBitStreamFilterContext *bsf = av_bitstream_filter_init("h264_mp4toannexb");

在您的解复用循环

AVPacket new_pkt = *pkt; int ret = av_bitstream_filter_filter(bsfc, avctx, NULL, &new_pkt.data, &new_pkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY); if (ret > 0) { // non-zero positive, you have new memory allocated, // keep it referenced in the AVBuffer av_free_packet(pkt); new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, av_buffer_default_free, NULL, 0); // handle memory error here } else if (ret < 0) { // handle failure here } // ret == 0, no problems, nothing else to do *pkt = new_pkt;

更多推荐

转换使用libav MP4,TS

本文发布于:2023-11-24 21:10:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:libav   TS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!