資源簡介
相應博客地址:http://blog.csdn.net/weixinhum/article/details/45872093
如有疑問請到博客留言,此外如有錯誤會在博客中更新
代碼片段和文件信息
#include?
extern?“C“//包含C文件頭
{
#include?“l(fā)ibavformat/avformat.h“
};
#define?DATASIZE?1024*1024
AVStream?*add_vidio_stream(AVFormatContext?*oc?enum?AVCodecID?codec_id)//用以初始化一個用于輸出的AVFormatContext結(jié)構(gòu)體
{
AVStream?*st;
AVCodec?*codec;
st?=?avformat_new_stream(oc?NULL);
if?(!st)
{
printf(“Could?not?alloc?stream\n“);
exit(1);
}
codec?=?avcodec_find_encoder(codec_id);//查找mjpeg解碼器
if?(!codec)
{
printf(“codec?not?found\n“);
exit(1);
}
avcodec_get_context_defaults3(st->codec?codec);//申請AVStream->codec(AVCodecContext對象)空間并設置默認值(由avcodec_get_context_defaults3()設置
st->codec->bit_rate?=?400000;//設置采樣參數(shù),即比特率??
st->codec->width?=?1080;//設置視頻寬高,這里跟圖片的寬高保存一致即可
st->codec->height?=?1800;
st->codec->time_base.den?=?10;//設置幀率
st->codec->time_base.num?=?1;
st->codec->pix_fmt?=?PIX_FMT_YUV420P;//設置像素格式??
st->codec->codec_tag?=?0;
if?(oc->oformat->flags?&?AVFMT_GLOBALHEADER)//一些格式需要視頻流數(shù)據(jù)頭分開
st->codec->flags?|=?CODEC_FLAG_GLOBAL_HEADER;
return?st;
}
void?main()
{
AVFormatContext?*ofmt_ctx?=?NULL;//其包含碼流參數(shù)較多,是一個貫穿始終的數(shù)據(jù)結(jié)構(gòu),很多函數(shù)都要用到它作為參數(shù)
const?char?*out_filename?=?“out.mkv“;//輸出文件路徑,在這里也可以將mkv改成別的ffmpeg支持的格式,如mp4,flv,avi之類的
int?ret;//返回標志
av_register_all();//初始化解碼器和復用器
avformat_alloc_output_context2(&ofmt_ctx?NULL?NULL?out_filename);//初始化一個用于輸出的AVFormatContext結(jié)構(gòu)體,視頻幀率和寬高在此函數(shù)里面設置
if?(!ofmt_ctx)
{
printf(“Could?not?create?output?context\n“);
return;
}
AVStream?*out_stream?=?add_vidio_stream(ofmt_ctx?AV_CODEC_ID_MJPEG);//創(chuàng)造輸出視頻流
av_dump_format(ofmt_ctx?0?out_filename?1);//該函數(shù)會打印出視頻流的信息,如果看著不開心可以不要
if?(!(ofmt_ctx->oformat->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);
return;
}
}
if?(avformat_write_header(ofmt_ctx?NULL)?0)//寫文件頭(Write?file?header)
{
printf(“Error?occurred?when?opening?output?file\n“);
return;
}
int?frame_index?=?0;//放入視頻的圖像計數(shù)
unsigned?char?*mydata?=?new?unsigned?char[DATASIZE];
AVPacket?pkt;
av_init_packet(&pkt);
pkt.flags?|=?AV_PKT_FLAG_KEY;
pkt.stream_index?=?out_stream->index;//獲取視頻信息,為壓入幀圖像做準備
while?(frame_index<100)//將圖像壓入視頻中
{
FILE?*file;//打開一張jpeg圖像并讀取其數(shù)據(jù),在這里圖像最大為1M如果超過1M,則需要修改1024*1024這里
fopen_s(&file?“1.jpg“?“rb“);
pkt.size?=?fread(mydata?1?DATASIZE?file);
pkt.data?=?mydata;
fclose(file);
if?(av_interleaved_write_frame(ofmt_ctx?&pkt)?0)?//寫入圖像到視頻
{
printf(“Error?muxing?packet\n“);
break;
}
printf(“Write?%8d?frames?to?output?file\n“?frame_index);//打印出當前壓入的幀數(shù)
frame_index++;
}
av_free_packet(&pkt);//釋放掉幀數(shù)據(jù)包對象
av_write_trailer(ofmt_ctx);//寫文件尾(Write?file?trailer)
delete[]mydata;//釋放數(shù)據(jù)對象
if?(ofmt_ctx?&&?!(ofmt_ctx->oformat->flags?&?AVFMT_NOFILE))
avio_close(ofmt_ctx->pb);//關閉視頻文件
avformat_free_context(ofmt_ctx);//釋放輸出視頻相關數(shù)據(jù)結(jié)構(gòu)
return;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-05-20?16:33??JpegToVideo\
?????目錄???????????0??2015-05-20?16:33??JpegToVideo\Debug\
?????目錄???????????0??2015-05-20?16:33??JpegToVideo\JpegToVideo\
?????文件?????????979??2015-05-20?15:56??JpegToVideo\JpegToVideo.sln
?????文件???????16896??2015-05-20?16:32??JpegToVideo\JpegToVideo.v12.suo
?????文件??????654124??2014-09-13?19:10??JpegToVideo\JpegToVideo\1.jpg
?????文件????18936320??2014-05-06?10:08??JpegToVideo\JpegToVideo\avcodec-55.dll
?????文件?????1340928??2014-05-06?10:08??JpegToVideo\JpegToVideo\avdevice-55.dll
?????文件?????2034688??2014-05-06?10:08??JpegToVideo\JpegToVideo\avfilter-4.dll
?????文件?????5342720??2014-05-06?10:08??JpegToVideo\JpegToVideo\avformat-55.dll
?????文件??????418304??2014-05-06?10:08??JpegToVideo\JpegToVideo\avutil-52.dll
?????目錄???????????0??2015-05-20?16:33??JpegToVideo\JpegToVideo\Debug\
?????目錄???????????0??2015-05-20?16:00??JpegToVideo\JpegToVideo\include\
?????文件????????5721??2012-05-23?20:03??JpegToVideo\JpegToVideo\include\inttypes.h
?????目錄???????????0??2015-05-20?16:00??JpegToVideo\JpegToVideo\include\libavcodec\
?????文件??????175612??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\avcodec.h
?????文件????????3111??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\avfft.h
?????文件????????2392??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\dxva2.h
?????文件???????10654??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\old_codec_ids.h
?????文件????????4007??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\vaapi.h
?????文件????????4094??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\vda.h
?????文件????????6200??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\vdpau.h
?????文件????????5593??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\version.h
?????文件????????6062??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavcodec\xvmc.h
?????目錄???????????0??2015-05-20?16:00??JpegToVideo\JpegToVideo\include\libavdevice\
?????文件???????16642??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavdevice\avdevice.h
?????文件????????1861??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavdevice\version.h
?????目錄???????????0??2015-05-20?16:00??JpegToVideo\JpegToVideo\include\libavfilter\
?????文件????????3321??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavfilter\asrc_abuffer.h
?????文件????????3784??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavfilter\avcodec.h
?????文件???????56887??2014-05-06?10:08??JpegToVideo\JpegToVideo\include\libavfilter\avfilter.h
............此處省略92個文件信息
評論
共有 條評論