資源簡(jiǎn)介
本程序是一個(gè)基于FFMPEG的封裝格式轉(zhuǎn)換器的源代碼。可以用于學(xué)習(xí)FFMPEG的開(kāi)發(fā)相關(guān)知識(shí)。
需要注意的是,本程序并不進(jìn)行視音頻的編碼和解碼工作。而是直接將視音頻壓縮碼流從一種封裝格式文件中獲取出來(lái)然后打包成另外一種封裝格式的文件。
支持FLV,AVI,TS,MP4等封裝格式之間的互相轉(zhuǎn)換。相比于轉(zhuǎn)碼程序,本程序有以下兩個(gè)特點(diǎn):
1.速度很快。
2.視音頻質(zhì)量無(wú)損
代碼片段和文件信息
/*?
?*最簡(jiǎn)單的基于FFmpeg的封裝格式轉(zhuǎn)換器
?*Simplest?FFmpeg?Remuxer
?*
?*雷霄驊?Lei?Xiaohua
?*leixiaohua1020@126.com
?*中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù)
?*Communication?University?of?China?/?Digital?TV?Technology
?*http://blog.csdn.net/leixiaohua1020
?*
?*本程序?qū)崿F(xiàn)了視頻封裝格式之間的轉(zhuǎn)換。
?*需要注意的是本程序并不改變視音頻的編碼格式。
?*
?*?This?software?converts?a?media?file?from?one?container?format
?*?to?another?container?format?without?encoding/decoding?video?files.
?*/
#include?“stdafx.h“
extern?“C“
{
#include?“l(fā)ibavformat/avformat.h“
};
int?_tmain(int?argc?_TCHAR*?argv[])
{
AVOutputFormat?*ofmt?=?NULL;
//輸入對(duì)應(yīng)一個(gè)AVFormatContext,輸出對(duì)應(yīng)一個(gè)AVFormatContext
//(Input?AVFormatContext?and?Output?AVFormatContext)
AVFormatContext?*ifmt_ctx?=?NULL?*ofmt_ctx?=?NULL;
AVPacket?pkt;
const?char?*in_filename?*out_filename;
int?ret?i;
if?(argc?3)?{
printf(“usage:?%s?input?output\n“
“Remux?a?media?file?with?libavformat?and?libavcodec.\n“
“The?output?format?is?guessed?according?to?the?file?extension.\n“
“Modified?by?Lei?Xiaohua?leixiaohua1020@126.com\n“
“Communication?University?of?China?/?Digital?TV?Technology\n“
“http://blog.csdn.net/leixiaohua1020“?argv[0]);
return?1;
}
in_filename??=?argv[1];//輸入文件名(Input?file?URL)
out_filename?=?argv[2];//輸出文件名(Output?file?URL)
av_register_all();
//輸入(Input)
if?((ret?=?avformat_open_input(&ifmt_ctx?in_filename?0?0))?0)?{
printf(?“Could?not?open?input?file.“);
goto?end;
}
if?((ret?=?avformat_find_stream_info(ifmt_ctx?0))?0)?{
printf(?“Failed?to?retrieve?input?stream?information“);
goto?end;
}
av_dump_format(ifmt_ctx?0?in_filename?0);
//輸出(Output)
avformat_alloc_output_context2(&ofmt_ctx?NULL?NULL?out_filename);
if?(!ofmt_ctx)?{
printf(?“Could?not?create?output?context\n“);
ret?=?AVERROR_UNKNOWN;
goto?end;
}
ofmt?=?ofmt_ctx->oformat;
for?(i?=?0;?i?nb_streams;?i++)?{
//根據(jù)輸入流創(chuàng)建輸出流(Create?output?AVStream?according?to?input?AVStream)
AVStream?*in_stream?=?ifmt_ctx->streams[i];
AVStream?*out_stream?=?avformat_new_stream(ofmt_ctx?in_stream->codec->codec);
if?(!out_stream)?{
printf(?“Failed?allocating?output?stream\n“);
ret?=?AVERROR_UNKNOWN;
goto?end;
}
//復(fù)制AVCodecContext的設(shè)置(Copy?the?settings?of?AVCodecContext)
ret?=?avcodec_copy_context(out_stream->codec?in_stream->codec);
if?(ret?0)?{
printf(?“Failed?to?copy?context?from?input?to?output?stream?codec?context\n“);
goto?end;
}
out_stream->codec->codec_tag?=?0;
if?(ofmt_ctx->oformat->flags?&?AVFMT_GLOBALHEADER)
out_stream->codec->flags?|=?CODEC_FLAG_GLOBAL_HEADER;
}
//輸出一下格式------------------
av_dump_format(ofmt_ctx?0?out_filename?1);
//打開(kāi)輸出文件(Open?output?file)
if?(!(ofmt->flags?&?AVFMT_NOFILE))?{
ret?=?avio_open(&ofmt_ctx->pb?out_filename?AVIO_FLAG_WRITE);
if?(ret?0)?{
printf(?“Could?not?open?output?file?‘%s‘“?out_filename);
goto?end;
}
}
//寫文件頭(Write?file?header)
ret?=?avformat_write_header(ofmt_ctx?N
- 上一篇:rtmpdump vs2005版本
- 下一篇:有關(guān)Qt的二十篇中文論文
評(píng)論
共有 條評(píng)論