ffmpeg ts列表合并为mp4

这篇具有很好参考价值的文章主要介绍了ffmpeg ts列表合并为mp4。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

操作系统:ubuntu
注意事项:
1.ts文件顺序必须正确,也就是下一帧的dst和pst要比上一帧的大,否则会报错
2.codecpar->codec_tag要设置为0,否则报错Tag [27][0][0][0] incompatible with output codec id ‘27’ (avc1)
3.设置output的max_streams数目,默认是1000,超过此数目就会报错文章来源地址https://www.toymoban.com/news/detail-640006.html


#include <iostream>
#include <dirent.h>
#include <vector>


extern "C" {
#include "include/libavformat/avformat.h"
#include "include/libavcodec/avcodec.h"
}

//#pragma comment(lib,"avformat.lib")
//#pragma comment(lib,"avcodec.lib")

using namespace std;

int ts2Mp4(const string,const string,int);

int main(int argc, char* argv[])
{
    //const string ts_filename = argv[1];
    //const string output_filename = "../1.mp4";//argv[2];
    ts2Mp4(argv[1],argv[2],atoi(argv[3]));

    return 0;
}

int ts2Mp4(const string ts_path,const string mp4_path,int ts_count){

    
    /*vector<string> ts_list;
    DIR *pDir;
    struct dirent* ptr;
    if(!(pDir = opendir(ts_path.c_str()))){
        printf("cannot open ts dir\n");
        return -1;
    }

      
    while((ptr = readdir(pDir))!=0) {
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name,"..")!=0){
               ts_list.push_back(ptr->d_name);
        }
    }
    closedir(pDir);*/


 // create output context 
    AVFormatContext* output_ctx = NULL;
    if (avformat_alloc_output_context2(&output_ctx, NULL, NULL, mp4_path.c_str()) < 0) {
        fprintf(stderr, "Failed to create output context\n");
        return -1;
    }

    //set the max streams number,default is 1000
    output_ctx->max_streams=3000;

    int video_stream_idx = -1;

    vector<AVFormatContext*> input_ctx_list;

    for(int i=0;i<ts_count;i++){
          // open input file 
        char ts[256];
        sprintf(ts,"%s%s%d%s",ts_path.c_str(),"/",i,".ts");
        //string ts=tmp;
        printf("%s\n",ts);
      

        if(strcmp(".ts",strstr(ts,".ts"))!=0) continue;
    

        AVFormatContext* input_ctx = NULL;
        if (avformat_open_input(&input_ctx, ts, NULL, NULL) != 0) {
            fprintf(stderr, "Failed to open input file '%s'\n", ts);
            return -1;
        }
        if (avformat_find_stream_info(input_ctx, NULL) < 0) {
            fprintf(stderr, "Failed to retrieve input stream information\n");
            return -1;
        }

        input_ctx_list.push_back(input_ctx);

        // add streams 
     for (int i = 0; i < input_ctx->nb_streams; i++) {
         AVStream* in_stream = input_ctx->streams[i];
         AVCodecParameters* in_codecpar = in_stream->codecpar;

         if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO || in_codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
             AVStream* out_stream = avformat_new_stream(output_ctx, NULL);
             if (!out_stream) {
                 fprintf(stderr, "Failed to allocate output stream\n");
                 return -1;
             }
             if (avcodec_parameters_copy(out_stream->codecpar, in_codecpar) < 0) {
                 fprintf(stderr, "Failed to copy codec parameters\n");
                 return -1;
             }

            out_stream->codecpar->codec_tag = 0;
           
             if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
                video_stream_idx = out_stream->index;
         }
     }

    }

     
     // open output file 
    if (!(output_ctx->oformat->flags & AVFMT_NOFILE)) {
        if (avio_open(&output_ctx->pb, mp4_path.c_str(), AVIO_FLAG_WRITE) < 0) {
            fprintf(stderr, "Could not open output file '%s'\n", mp4_path.c_str());
            return -1;
        }
    }
    // write header 
    if (avformat_write_header(output_ctx, NULL) < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        return -1;
    }

      int pkt_cnt = 0;
        // copy packets 
        
        //int stream_index = 0;

     for(int i=0;i<input_ctx_list.size();i++){
        AVFormatContext* input_ctx=input_ctx_list[i];
         AVPacket packet;
        int ret = 0;


        while (av_read_frame(input_ctx, &packet) >= 0) {
            //printf("%d,%d\n",video_stream_idx,packet.stream_index);
            AVStream* in_stream = input_ctx->streams[packet.stream_index];
            AVStream* out_stream = output_ctx->streams[packet.stream_index];

            // copy packet
            packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base,
                (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
            packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base,
                (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
            packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);

            //printf("%ld,%ld,%ld\n",packet.pts,packet.dts,packet.duration);

            packet.pos = -1;
            if (packet.stream_index == video_stream_idx) {
                //printf("Send video %8d\n", pkt_cnt);
                pkt_cnt++;
            }
            
            ret = av_interleaved_write_frame(output_ctx, &packet);
            if (ret < 0) {
                fprintf(stderr, "Error muxing packet\n");
                break;
            }
            av_packet_unref(&packet);
        }
          // close input 
        avformat_close_input(&input_ctx);
    }


 // write trailer 
    if (av_write_trailer(output_ctx) < 0) {
       fprintf(stderr, "Error occurred when writing trailer\n");
       return -1;
    }
  
    // close output 
    if (output_ctx && !(output_ctx->oformat->flags & AVFMT_NOFILE))
        avio_closep(&output_ctx->pb);
    avformat_free_context(output_ctx);

    printf("convert success!\n"); 


    return 0;
}


到了这里,关于ffmpeg ts列表合并为mp4的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 不用FFMpeg,用Python代码解密ts文件还原mp4视频(前提:有正确的key)

    好不容易下载了.ts视频,但内容全被加密。要解密的前提条件是必须先有正确的key文件,然后对.ts文件进行AES-128解密,还原成.mp4视频。 以往的做法是用FFMpeg命令: 现在可以用Python代码直接解密。 非常感谢这篇文章给我参考的灵感: 流媒体m3u8爬虫研究 - Echocipher 首先Python要

    2024年02月02日
    浏览(36)
  • python ffmpeg将mp4文件实时转码为ts,并指定pid等信息,输出到udp

    要将MP4文件实时转码为TS格式,并将PID等信息指定为UDP输出,可以使用 subprocess 模块和ffmpeg命令行工具来实现。以下是一个示例代码,用于实时转码并将输出发送到UDP服务器: 在上述代码中,我们首先定义了输入文件、UDP服务器地址和PID等信息。然后,我们使用 subprocess.Pop

    2024年01月22日
    浏览(37)
  • 如何使用ffmpeg将BDMV(m2ts)转换成MKV、MP4等其他格式的文件

    BDMV 是蓝光碟使用的格式。这种格式没有办法使用播放软件播放,必须要用硬盘播放器,也就是专门的设备。但是最经典的 ffmpeg 可以将其转换成其他格式,并且保持相同的码率和清晰度,这样就可以很方便的查看了。 本文使用 macOS 进行演示,但是会介绍如何一些其他平台的

    2024年02月10日
    浏览(54)
  • python ts视频转mp4

    TS(TransportStream,传输流)是一种封装的格式,它的全称为MPEG2-TS。MPEG2-TS是一种标准数据容器格式,传输与存储音视频、节目与系统信息协议数据,主要应用于数字广播系统,譬如DVB、ATSC与IPTV。传输流最初是为广播而设计的。后来,通过在标准的188字节数据包中添加4字节的

    2024年02月07日
    浏览(32)
  • FFMPEG mp4封装实现

    FFMPEG mp4录像 author:lyn date:2022.09.28 version: ffmpeg4.1.3 1.mp4数据结构 2.ffmpeg mp4封装实现 3.mp4函数调用关系 4.参考资料 1.mp4数据结构 1.1mp4简介 MP4或称MPEG-4第14部分(英语:MPEG-4 Part 14)是一种标准的数字多媒体容器格式。MPEG-4第14部分的扩展名为 .mp4 ,以存储数字音频及数字视频

    2023年04月08日
    浏览(18)
  • ffmpeg操作MP4视频封面

    提取视频封面 视频流中提取帧图 3.重新设置视频封面 更多参考: https://blog.csdn.net/m0_37624402/article/details/125123818

    2024年02月04日
    浏览(31)
  • 20231005使用ffmpeg旋转MP4视频

    20231005使用ffmpeg旋转MP4视频 2023/10/5 12:21 百度搜搜:ffmpeg 旋转90度 https://zhuanlan.zhihu.com/p/637790915 【FFmpeg实战】FFMPEG常用命令行 https://blog.csdn.net/weixin_37515325/article/details/127817057 FFMPEG常用命令行 5.视频旋转 顺时针旋转90度:ffmpeg -i test.mp4 -vf \\\"transpose=1\\\" out.mp4//顺时针旋转90° 逆时针

    2024年02月07日
    浏览(31)
  • ffmpeg将rtsp流转成mp4

    中间的rtsp网址一定要加上双引号,避免出现url有特殊字符的问题 如果不支持tcp协议,去掉下面两个参数即可,加上这两个参数是因为ffmpeg默认使用udp协议,会导致丢包 -rtsp_transport、-tcp

    2024年02月11日
    浏览(30)
  • ffmpeg批量转换mpg为mp4

    1、新建一个txt文件,并复制如下代码进入,然后保存。 2、把文件后缀修改为bat 。 3、把后缀为bat的文件放到要批量处理的视频文件夹里面。 4、在确保安装了ffmpeg的情况下,双击bat文件执行即可。 5、参数说明: -i          输入文件,这里指的就是视频文件。 -y       

    2024年02月13日
    浏览(31)
  • FFmpeg将编码后数据保存成mp4

          以下测试代码实现的功能是:持续从内存块中获取原始数据,然后依次进行解码、编码、最后保存成mp4视频文件。       可保存成单个视频文件,也可指定每个视频文件的总帧数,保存多个视频文件。       为了便于查看和修改,这里将可独立的程序段存放在单个函

    2024年02月13日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包