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,超過此數目就會報錯


#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 1000output_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 packetpacket.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;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/35278.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/35278.shtml
英文地址,請注明出處:http://en.pswp.cn/news/35278.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

docker版jxTMS使用指南:使用jxTMS采集數據之二

本文是如何用jxTMS進行數據采集的第二部分&#xff0c;整個系列的文章請查看&#xff1a;docker版jxTMS使用指南&#xff1a;4.4版升級內容 docker版本的使用&#xff0c;請查看&#xff1a;docker版jxTMS使用指南 4.0版jxTMS的說明&#xff0c;請查看&#xff1a;4.0版升級內…

Vue + MapBox快速搭建

一、說明&#xff1a; 1.mapbox-gl自2.0版本開始不再開源&#xff0c;需要用戶在官網申請key使用。 2.maplibre GL JS是一個開源庫&#xff0c;它起源于 mapbox-gl-js 的開源分支。該庫的初始版本&#xff08;1.x&#xff09;旨在替代Mapbox的OSS版本。簡單來說maplibre是mapb…

異步場景加載詳解

異步場景加載詳解 介紹 異步場景加載是一種在Unity中加載場景的方式&#xff0c;它允許在加載過程中執行其他操作&#xff0c;并提供了加載進度的反饋。通過異步加載&#xff0c;可以避免加載大型場景時的卡頓現象&#xff0c;提高游戲的流暢性和用戶體驗。 方法 在Unity中…

C++——缺省參數

缺省參數的定義 缺省參數是聲明或定義函數時為函數的參數指定一個缺省值。在調用該函數的時候&#xff0c;如果沒有指定實參&#xff0c;則采用該形參的缺省值&#xff0c;否則使用指定的實參。 void Func(int a 0) {cout << a << endl; } int main() { Func()…

【Kubernetes】Kubernetes之Pod詳解

Pod 一、 Pod1. Pod 基礎概念2. 在 Kubrenetes 集群中 Pod 使用方式2.1 pasue 容器2.2 kubernetes 中的 pause 容器提供的功能 3. Pod 的概念和結構組成4. Pod 的分類5. Pod 容器的分類5.1 基礎容器&#xff08;infrastructure container&#xff09;5.2 初始化容器&#xff08…

07 |「異步任務」

前言 實踐是最好的學習方式&#xff0c;技術也如此。 文章目錄 前言一、進程與線程1、進程2、線程 二、實現三、異步任務加載器 一、進程與線程 1、進程 進程(Process)是操作系統分配資源的基本單位,它是一個執行中的程序實例&#xff1b;每個進程都有自己獨立的內存空間,不同…

【大數據】Flink 詳解(二):核心篇 Ⅲ

Flink 詳解&#xff08;二&#xff09;&#xff1a;核心篇 Ⅲ 29、Flink 通過什么實現可靠的容錯機制&#xff1f; Flink 使用 輕量級分布式快照&#xff0c;設計檢查點&#xff08;checkpoint&#xff09;實現可靠容錯。 30、什么是 Checkpoin 檢查點&#xff1f; Checkpoint …

百度 amis 當成 UI 庫用

百度 amis 當成 UI 庫用 1.獲取到這些 amis 對外提供的方法 var amisLib amisRequire(amis);// 獲取到這些 amis 對外提供的方法。 2.js中使用百度amis中 confirm var name"name";amisLib.confirm(請確認刪除 name !,"刪除").then((confirmed) > {if…

如何進行游戲平臺搭建?

游戲平臺搭建涉及多個步驟和技術&#xff0c;下面是一個大致的指南&#xff1a; 市場調研和定位&#xff1a;首先&#xff0c;要了解游戲市場和受眾的需求&#xff0c;選擇適合的游戲類型和定位。 選擇平臺類型&#xff1a;決定是要搭建網頁平臺、移動應用平臺還是其他類型的…

群暉6.X便捷的安裝cpolar內網穿透

群暉6.X便捷的安裝cpolar內網穿透 文章目錄 群暉6.X便捷的安裝cpolar內網穿透前言1. 下載cpolar的群暉套件1.1 打開群暉套件中心1.2 選擇“手動安裝”1.3 選擇下載cpolar套件位置 2. 打開cpolar的Web-UI界面3. 注冊會員 前言 隨著硬件設備和軟件技術的發展&#xff0c;以及數據…

概率圖模型(Probabilistic Graphical Model,PGM)

概率圖模型&#xff08;Probabilistic Graphical Model&#xff0c;PGM&#xff09;&#xff0c;是一種用圖結構來描述多元隨機變量之間條件獨立性的概率模型。它可以用來表示復雜的概率分布&#xff0c;進行有效的推理和學習&#xff0c;以及解決各種實際問題&#xff0c;如圖…

Redis小例子

MAC電腦下Redis的安裝&#xff1a; brew install redis下面給一個Java操作redis的小例子 import redis.clients.jedis.Jedis;public class Demo {public static void main(String[] args) {// 創建 Jedis 客戶端實例&#xff0c;連接到本地 Redis 服務器&#xff0c;默認端口…

RedisDesktopManage

RDM 簡介下載安裝 簡介 RedisDesktopManager&#xff08;RDM&#xff09;是一個開源的跨平臺圖形界面工具&#xff0c;用于管理和操作 Redis 數據庫。它提供了一個用戶友好的界面&#xff0c;使用戶能夠輕松地連接、瀏覽、查詢和修改 Redis 數據&#xff0c;而無需使用命令行界…

教你10分鐘內學習如何CSS 設置網頁打印時的樣式

本文將教您開始為要打印的頁面編寫CSS所需要的一切提供幫助。 media 規則 If you’ve done any responsive design, you’ll already know about the media rule. As well as different screen sizes, media also lets you target “print” media. Here’s an example: 如果…

競賽項目 深度學習的動物識別

文章目錄 0 前言1 背景2 算法原理2.1 動物識別方法概況2.2 常用的網絡模型2.2.1 B-CNN2.2.2 SSD 3 SSD動物目標檢測流程4 實現效果5 部分相關代碼5.1 數據預處理5.2 構建卷積神經網絡5.3 tensorflow計算圖可視化5.4 網絡模型訓練5.5 對貓狗圖像進行2分類 6 最后 0 前言 &#…

【Java】一只小菜坤的編程題之旅【3】

文章目錄 1丶判定是否互為字符重排2、楊輝三角3丶某公司的1個面試題&#xff08;字符串包含問題&#xff09; 1丶判定是否互為字符重排 這個題我們用一個非常簡單的思想就能實現&#xff0c;我們先將字符串轉換為字符數組&#xff0c;然后對字符數組進行排序&#xff0c;然后再…

el-radio單選框,取消選中

1.背景&#xff1a;在公司開發需求中有一個選擇顏色的單選框&#xff08;黑色&#xff0c;白色&#xff09;&#xff0c;每種顏色選擇后均支持取消選中&#xff0c;可是el-radio標簽不支持取消選中。 2.解決&#xff1a; 方法1: <el-radio-group v-model"radioColo…

【Apollo】自動駕駛的平臺背景,平臺介紹

作者簡介&#xff1a; 辭七七&#xff0c;目前大一&#xff0c;正在學習C/C&#xff0c;Java&#xff0c;Python等 作者主頁&#xff1a; 七七的個人主頁 文章收錄專欄&#xff1a; 七七的閑談 歡迎大家點贊 &#x1f44d; 收藏 ? 加關注哦&#xff01;&#x1f496;&#x1f…

spring boot 集成 jetcache【基礎篇:@Cached、@CreateCache、@CacheRefresh】

手打不易&#xff0c;如果轉摘&#xff0c;請注明出處&#xff01; 注明原文&#xff1a;https://zhangxiaofan.blog.csdn.net/article/details/129832925 目錄 前言 版本 配置通用說明 項目結構 代碼 啟動類 實體類 基礎使用——增刪改查&#xff08;Cached、CacheInv…

opencv實戰項目 手勢識別-手勢控制鍵盤

手勢識別是一種人機交互技術&#xff0c;通過識別人的手勢動作&#xff0c;從而實現對計算機、智能手機、智能電視等設備的操作和控制。 1. opencv實現手部追蹤&#xff08;定位手部關鍵點&#xff09; 2.opencv實戰項目 實現手勢跟蹤并返回位置信息&#xff08;封裝調用&am…