資源簡介
使用ffmpeg庫把mp4視頻文件保存為一幀一幀的JPG格式圖片
代碼片段和文件信息
/*
*最簡單的音頻轉碼器(只處理音頻)
*繆國凱?Mickel
*821486004@qq.com
*本程序實現從一個視頻格式轉碼到另一個視頻格式,只處理音頻,視頻忽略,若有多個音頻流,只處理第一個采用swr_convert重取樣
*2015-5-8
*/
#include?“stdafx.h“
#ifdef?__cplusplus
extern“C“
{
#endif
#include?
#include?“libavcodec/avcodec.h“
#include?“libavfilter/avfiltergraph.h“
#include?“libavfilter/buffersink.h“
#include?“libavfilter/buffersrc.h“
#include?“libavutil/avutil.h“
#include?“libavutil/opt.h“
#include?“libavutil/pixdesc.h“
#include?“libswresample\swresample.h“
#include?“libavutil\fifo.h“
#include?
#include?
#pragma?comment(lib?“avcodec.lib“)
#pragma?comment(lib?“avformat.lib“)
#pragma?comment(lib?“avutil.lib“)
//#pragma?comment(lib?“avdevice.lib“)
#pragma?comment(lib?“avfilter.lib“)
//#pragma?comment(lib?“postproc.lib“)
#pragma?comment(lib?“swresample.lib“)
//#pragma?comment(lib?“swscale.lib“)
#ifdef?__cplusplus
};
#endif
#define?__STDC_CONSTANT_MACROS
#ifdef?_WIN32
//Windows
extern?“C“
{
#include?“libavcodec/avcodec.h“
#include?“libavformat/avformat.h“
#include?“libswscale/swscale.h“
//#include?“SDL.h“
};
#else
//Linux...
#ifdef?__cplusplus
extern?“C“
{
#endif
#include?
#include?
#include?
#include?
#ifdef?__cplusplus
};
#endif
#endif
#define?MAX_PATH?128
#include?“stdio.h“
/**
*?將AVframe(YUV420格式)保存為JPEG格式的圖片
*
*?@param?width?YUV420的寬
*?@param?height?YUV42的高
*
*/
int?MyWriteJPEG(AVframe*?pframe?int?width?int?height?int?iIndex)
{
//?輸出文件路徑
char?out_file[MAX_PATH]?=?{0};
//???sprintf_s(out_file?sizeof(out_file)?“./%d.jpg“??iIndex);
sprintf(out_file“./tupian/%d.jpg“iIndex);
//?分配AVFormatContext對象
AVFormatContext*?pFormatCtx?=?avformat_alloc_context();
//?設置輸出文件格式
pFormatCtx->oformat?=?av_guess_format(“mjpeg“?NULL?NULL);
//?創建并初始化一個和該url相關的AVIOContext
if(?avio_open(&pFormatCtx->pb?out_file?AVIO_FLAG_READ_WRITE)?0)
{
printf(“Couldn‘t?open?output?file.“);
return?-1;
}
//?構建一個新stream
AVStream*?pAVStream?=?avformat_new_stream(pFormatCtx?0);
if(?pAVStream?==?NULL?)
{
return?-1;
}
//?設置該stream的信息
AVCodecContext*?pCodecCtx?=?pAVStream->codec;
pCodecCtx->codec_id?=?pFormatCtx->oformat->video_codec;
pCodecCtx->codec_type?=?AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt?=?PIX_FMT_YUVJ420P;
pCodecCtx->width?=?width;
pCodecCtx->height?=?height;
pCodecCtx->time_base.num?=?1;
pCodecCtx->time_base.den?=?25;
//?Begin?Output?some?information
av_dump_format(pFormatCtx?0?out_file?1);
//?End?Output?some?information
//?查找解碼器
AVCodec*?pCodec?=?avcodec_find_encoder(pCodecCtx->codec_id);
if(?!pCodec?)
{
printf(“Codec?not?found.“);
return?-1;
}
//?設置pCodecCtx的解碼器為pCodec
if(?avcodec_open2(pCodecCtx?pCodec?NULL)?0?)?
{
printf(“Could?not?open?codec.“);
return?-1;
}
//Write?Header
avformat_write_header(pFormatCtx?N
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????2686976??2018-03-22?11:23??Mp4ToJPG\ipch\mp4tojpg-568c8c52\mp4tojpg-c11bbb81.ipch
?????文件????2686976??2018-03-21?15:40??Mp4ToJPG\ipch\transcodingaudiouseswrcontext-78e17787\transcodingaudiouseswrcontext-8be9c8c8.ipch
?????文件????2162688??2017-08-15?17:29??Mp4ToJPG\ipch\transcodingaudiouseswrcontext-b668ddf6\transcodingaudiouseswrcontext-8be9c8c8.ipch
?????文件????2686976??2017-08-15?17:32??Mp4ToJPG\ipch\transcodingaudiouseswrcontext-fec6938e\transcodingaudiouseswrcontext-8be9c8c8.ipch
?????文件???18936320??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\avcodec-55.dll
?????文件????1340928??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\avdevice-55.dll
?????文件????2034688??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\avfilter-4.dll
?????文件????5342720??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\avformat-55.dll
?????文件?????418304??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\avutil-52.dll
?????文件???????8908??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\befor_swr.txt
?????文件????1709108??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\cuc_ieschool.ts
?????文件???????1222??2018-03-22?11:25??Mp4ToJPG\Mp4ToJPG\Debug\Mp4ToJPG.Build.CppClean.log
?????文件?????????95??2018-03-22?11:25??Mp4ToJPG\Mp4ToJPG\Debug\Mp4ToJPG.log
?????文件????1393457??2017-08-29?16:46??Mp4ToJPG\Mp4ToJPG\demo.mp4
?????文件???????9216??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\end_swr.txt
?????文件???????5721??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\inttypes.h
?????文件?????175612??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\avcodec.h
?????文件???????3111??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\avfft.h
?????文件???????2392??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\dxva2.h
?????文件??????10654??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\old_codec_ids.h
?????文件???????4007??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\vaapi.h
?????文件???????4094??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\vda.h
?????文件???????6200??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\vdpau.h
?????文件???????5593??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\version.h
?????文件???????6062??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavcodec\xvmc.h
?????文件??????16642??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavdevice\avdevice.h
?????文件???????1861??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavdevice\version.h
?????文件???????3321??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavfilter\asrc_abuffer.h
?????文件???????3784??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavfilter\avcodec.h
?????文件??????56887??2017-08-15?17:29??Mp4ToJPG\Mp4ToJPG\include\libavfilter\avfilter.h
............此處省略116個文件信息
- 上一篇:半導體器件物理-施敏
- 下一篇:ACM圖靈獎演講集:前20年
評論
共有 條評論