最簡單的基於FFMPEG的封裝格式轉換器(無編解碼)

這個也是比較常用的,比如我要從MP4文件轉成avi文件。 注意此時封裝格式的轉換,即不進行轉碼工作,相當於文件內部原來是h.264的,還是h.264,只是外面的殼變了。


原文地址:http://blog.csdn.net/leixiaohua1020/article/details/25422685

=====================================================

最簡單的基於FFmpeg的封裝格式處理系列文章列表:

最簡單的基於FFmpeg的封裝格式處理:視音頻分離器簡化版(demuxer-simple)

最簡單的基於FFmpeg的封裝格式處理:視音頻分離器(demuxer)

最簡單的基於FFmpeg的封裝格式處理:視音頻複用器(muxer)

最簡單的基於FFMPEG的封裝格式處理:封裝格式轉換(remuxer)

=====================================================

簡介

本文介紹一個基於FFMPEG的封裝格式轉換器。所謂的封裝格式轉換,就是在AVI,FLV,MKV,MP4這些格式之間轉換(對應.avi,.flv,.mkv,.mp4文件)。需要注意的是,本程序並不進行視音頻的編碼和解碼工作。而是直接將視音頻壓縮碼流從一種封裝格式文件中獲取出來然後打包成另外一種封裝格式的文件。傳統的轉碼程序工作原理如下圖所示:

上圖例舉了一個舉例:FLV(視頻:H.264,音頻:AAC)轉碼爲AVI(視頻:MPEG2,音頻MP3)的例子。可見視頻轉碼的過程通俗地講相當於把視頻和音頻重新“錄”了一遍。
本程序的工作原理如下圖所示:


由圖可見,本程序並不進行視頻和音頻的編解碼工作,因此本程序和普通的轉碼軟件相比,有以下兩個特點:
處理速度極快。視音頻編解碼算法十分複雜,佔據了轉碼的絕大部分時間。因爲不需要進行視音頻的編碼和解碼,所以節約了大量的時間。

視音頻質量無損。因爲不需要進行視音頻的編碼和解碼,所以不會有視音頻的壓縮損傷。

流程(2014.9.29更新)

下面附上基於FFmpeg的Remuxer的流程圖。圖中使用淺紅色標出了關鍵的數據結構,淺藍色標出了輸出視頻數據的函數。可見成個程序包含了對兩個文件的處理:讀取輸入文件(位於左邊)和寫入輸出文件(位於右邊)。中間使用了一個avcodec_copy_context()拷貝輸入的AVCodecContext到輸出的AVCodecContext。


簡單介紹一下流程中關鍵函數的意義:

輸入文件操作:

avformat_open_input():打開輸入文件,初始化輸入視頻碼流的AVFormatContext。

av_read_frame():從輸入文件中讀取一個AVPacket。

輸出文件操作:

avformat_alloc_output_context2():初始化輸出視頻碼流的AVFormatContext。

avformat_new_stream():創建輸出碼流的AVStream。

avcodec_copy_context():拷貝輸入視頻碼流的AVCodecContex的數值t到輸出視頻的AVCodecContext。

avio_open():打開輸出文件。

avformat_write_header():寫文件頭(對於某些沒有文件頭的封裝格式,不需要此函數。比如說MPEG2TS)。

av_interleaved_write_frame():將AVPacket(存儲視頻壓縮碼流數據)寫入文件。

av_write_trailer():寫文件尾(對於某些沒有文件頭的封裝格式,不需要此函數。比如說MPEG2TS)。

代碼

貼上代碼,代碼是從FFmpeg的例子改編的,平臺是VC2010。

  1. /* 
  2.  *最簡單的基於FFmpeg的封裝格式轉換器 
  3.  *Simplest FFmpeg Remuxer 
  4.  * 
  5.  *雷霄驊 Lei Xiaohua 
  6.  *[email protected] 
  7.  *中國傳媒大學/數字電視技術 
  8.  *Communication University of China / Digital TV Technology 
  9.  *http://blog.csdn.net/leixiaohua1020 
  10.  * 
  11.  *本程序實現了視頻封裝格式之間的轉換。 
  12.  *需要注意的是本程序並不改變視音頻的編碼格式。 
  13.  * 
  14.  * This software converts a media file from one container format 
  15.  * to another container format without encoding/decoding video files. 
  16.  */  
  17.    
  18. #include "stdafx.h"  
  19.    
  20. extern "C"  
  21. {  
  22. #include "libavformat/avformat.h"  
  23. };  
  24.    
  25.    
  26. int _tmain(int argc, _TCHAR* argv[])  
  27. {  
  28.     AVOutputFormat *ofmt = NULL;  
  29.     //輸入對應一個AVFormatContext,輸出對應一個AVFormatContext  
  30.     //(Input AVFormatContext and Output AVFormatContext)  
  31.     AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;  
  32.     AVPacket pkt;  
  33.     const char *in_filename, *out_filename;  
  34.     int ret, i;  
  35.     if (argc < 3) {  
  36.         printf("usage: %s input output\n"  
  37.             "Remux a media file with libavformat and libavcodec.\n"  
  38.             "The output format is guessed according to the file extension.\n"  
  39.             "Modified by Lei Xiaohua, [email protected]\n"  
  40.             "Communication University of China / Digital TV Technology\n"  
  41.             "http://blog.csdn.net/leixiaohua1020", argv[0]);  
  42.         return 1;  
  43.     }  
  44.     in_filename  = argv[1];//輸入文件名(Input file URL)  
  45.     out_filename = argv[2];//輸出文件名(Output file URL)  
  46.     av_register_all();  
  47.     //輸入(Input)  
  48.     if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {  
  49.         printf( "Could not open input file.");  
  50.         goto end;  
  51.     }  
  52.     if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {  
  53.         printf( "Failed to retrieve input stream information");  
  54.         goto end;  
  55.     }  
  56.     av_dump_format(ifmt_ctx, 0, in_filename, 0);  
  57.     //輸出(Output)  
  58.     avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);  
  59.     if (!ofmt_ctx) {  
  60.         printf( "Could not create output context\n");  
  61.         ret = AVERROR_UNKNOWN;  
  62.         goto end;  
  63.     }  
  64.     ofmt = ofmt_ctx->oformat;  
  65.     for (i = 0; i < ifmt_ctx->nb_streams; i++) {  
  66.         //根據輸入流創建輸出流(Create output AVStream according to input AVStream)  
  67.         AVStream *in_stream = ifmt_ctx->streams[i];  
  68.         AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);  
  69.         if (!out_stream) {  
  70.             printf( "Failed allocating output stream\n");  
  71.             ret = AVERROR_UNKNOWN;  
  72.             goto end;  
  73.         }  
  74.         //複製AVCodecContext的設置(Copy the settings of AVCodecContext)  
  75.         ret = avcodec_copy_context(out_stream->codec, in_stream->codec);  
  76.         if (ret < 0) {  
  77.             printf( "Failed to copy context from input to output stream codec context\n");  
  78.             goto end;  
  79.         }  
  80.         out_stream->codec->codec_tag = 0;  
  81.         if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)  
  82.             out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  83.     }  
  84.     //輸出一下格式------------------  
  85.     av_dump_format(ofmt_ctx, 0, out_filename, 1);  
  86.     //打開輸出文件(Open output file)  
  87.     if (!(ofmt->flags & AVFMT_NOFILE)) {  
  88.         ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);  
  89.         if (ret < 0) {  
  90.             printf( "Could not open output file '%s'", out_filename);  
  91.             goto end;  
  92.         }  
  93.     }  
  94.     //寫文件頭(Write file header)  
  95.     ret = avformat_write_header(ofmt_ctx, NULL);  
  96.     if (ret < 0) {  
  97.         printf( "Error occurred when opening output file\n");  
  98.         goto end;  
  99.     }  
  100.     int frame_index=0;  
  101.     while (1) {  
  102.         AVStream *in_stream, *out_stream;  
  103.         //獲取一個AVPacket(Get an AVPacket)  
  104.         ret = av_read_frame(ifmt_ctx, &pkt);  
  105.         if (ret < 0)  
  106.             break;  
  107.         in_stream  = ifmt_ctx->streams[pkt.stream_index];  
  108.         out_stream = ofmt_ctx->streams[pkt.stream_index];  
  109.         /* copy packet */  
  110.         //轉換PTS/DTS(Convert PTS/DTS)  
  111.         pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  112.         pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  113.         pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);  
  114.         pkt.pos = -1;  
  115.         //寫入(Write)  
  116.         ret = av_interleaved_write_frame(ofmt_ctx, &pkt);  
  117.         if (ret < 0) {  
  118.             printf( "Error muxing packet\n");  
  119.             break;  
  120.         }  
  121.         printf("Write %8d frames to output file\n",frame_index);  
  122.         av_free_packet(&pkt);  
  123.         frame_index++;  
  124.     }  
  125.     //寫文件尾(Write file trailer)  
  126.     av_write_trailer(ofmt_ctx);  
  127. end:  
  128.     avformat_close_input(&ifmt_ctx);  
  129.     /* close output */  
  130.     if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))  
  131.         avio_close(ofmt_ctx->pb);  
  132.     avformat_free_context(ofmt_ctx);  
  133.     if (ret < 0 && ret != AVERROR_EOF) {  
  134.         printf( "Error occurred.\n");  
  135.         return -1;  
  136.     }  
  137.     return 0;  
  138. }  

調試的時候,只需要“右鍵工程->調試->命令行參數”裏面設置輸入的文件名和輸出的文件名就可以了。

結果

下圖顯示了一個測試的輸入文件的視音頻參數。

 
下圖顯示了輸出文件的視音頻參數。可以看出除了視頻的封裝格式從flv轉換成了mp4,其他有關視音頻編碼的參數沒有任何變化。



下載


simplest ffmpeg format


項目主頁

SourceForge:https://sourceforge.net/projects/simplestffmpegformat/

Github:https://github.com/leixiaohua1020/simplest_ffmpeg_format

開源中國:http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_format


CSDN下載:
http://download.csdn.net/detail/leixiaohua1020/8005317

工程中包含4個例子:

simplest_ffmpeg_demuxer_simple:視音頻分離器(簡化版)。

simplest_ffmpeg_demuxer:視音頻分離器。

simplest_ffmpeg_muxer:視音頻複用器。

simplest_ffmpeg_remuxer:封裝格式轉換器。


更新-1.1==================================================

修復了以下問題:
(1)Release版本下的運行問題
(2)simplest_ffmpeg_muxer封裝H.264裸流的時候丟失聲音的錯誤

CSDN下載地址:

http://download.csdn.net/detail/leixiaohua1020/8284309


更新-1.2 (2015.2.13)=========================================

這次考慮到了跨平臺的要求,調整了源代碼。經過這次調整之後,源代碼可以在以下平臺編譯通過:

VC++:打開sln文件即可編譯,無需配置。

cl.exe:打開compile_cl.bat即可命令行下使用cl.exe進行編譯,注意可能需要按照VC的安裝路徑調整腳本里面的參數。編譯命令如下。

  1. ::VS2010 Environment  
  2. call "D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"  
  3. ::include  
  4. @set INCLUDE=include;%INCLUDE%  
  5. ::lib  
  6. @set LIB=lib;%LIB%  
  7. ::compile and link  
  8. cl simplest_ffmpeg_remuxer.cpp /link avcodec.lib avformat.lib avutil.lib ^  
  9. avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib /OPT:NOREF  

MinGW:MinGW命令行下運行compile_mingw.sh即可使用MinGW的g++進行編譯。編譯命令如下。

  1. g++ simplest_ffmpeg_remuxer.cpp -g -o simplest_ffmpeg_remuxer.exe \  
  2. -I /usr/local/include -L /usr/local/lib -lavformat -lavcodec -lavutil  

GCC:Linux或者MacOS命令行下運行compile_gcc.sh即可使用GCC進行編譯。編譯命令如下。

  1. gcc simplest_ffmpeg_remuxer.cpp -g -o simplest_ffmpeg_remuxer.out -I /usr/local/include -L /usr/local/lib \  
  2. -lavformat -lavcodec -lavutil  

PS:相關的編譯命令已經保存到了工程文件夾中

CSDN下載地址:http://download.csdn.net/detail/leixiaohua1020/8445303

SourceForge上已經更新。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章