資源簡介
基于ffmpeg將avi視頻轉換為mp4視頻,先將avi轉為yuv420p視頻幀序列,后將視頻幀序列轉為mp4視頻
代碼片段和文件信息
extern?“C“
{
#include?“libavformat\avformat.h“
#include?“libavdevice\avdevice.h“
#include?“libswscale\swscale.h“
};
int?pNewWidth?=?0;
int?pNewHeight?=?0;
void?SetVideoframe(AVCodecContext?*pCodecCtx?AVframe?*pframe?AVframe?*pNewframe?int?pNewWidth?int?pNewHeight)
{
pNewframe->linesize[0]?=?pNewWidth;
pNewframe->linesize[1]?=?pNewWidth?/?2;
pNewframe->linesize[2]?=?pNewWidth?/?2;
struct?SwsContext?*pSwsCtx?=?NULL;
pSwsCtx?=?sws_getContext(pCodecCtx->width?pCodecCtx->height?PIX_FMT_YUV420P?pNewWidth?pNewHeight?PIX_FMT_YUV420P?SWS_SINC?NULL?NULL?NULL);
if?(pSwsCtx?==?NULL)
return;
sws_scale(pSwsCtx?pframe->data?pframe->linesize?0?pCodecCtx->height?pNewframe->data?pNewframe->linesize);
sws_freeContext(pSwsCtx);
}
int?VideoDecode(char?*VideoName?char*?YUVName)
{
FILE?*fw?=?fopen(YUVName?“wb“);
if?(fw?==?NULL)
return?-1;
AVFormatContext?*pFormatContext?=?NULL;
pFormatContext?=?avformat_alloc_context();
if?(avformat_open_input(&pFormatContext?VideoName?NULL?NULL)?!=?0)
return?-2;
if?(avformat_find_stream_info(pFormatContext?NULL)?0)
{
avformat_close_input(&pFormatContext);
return?-3;
}
AVCodecContext?*pCodecContext?=?NULL;
pCodecContext?=?pFormatContext->streams[0]->codec;
AVCodec?*pCodec?=?NULL;
pCodec?=?avcodec_find_decoder(pCodecContext->codec_id);
if?(pCodec?==?NULL)
{
avformat_close_input(&pFormatContext);
return?-4;
}
if?(avcodec_open2(pCodecContext?pCodec?NULL)?0)
{
avformat_close_input(&pFormatContext);
return?-5;
}
AVframe?*pframe?*pframeDispose;
pframe?=?av_frame_alloc();
pframeDispose?=?av_frame_alloc();
if?((pframe?==?NULL)?||?(pframeDispose?==?NULL))
{
avcodec_close(pCodecContext);
avformat_close_input(&pFormatContext);
return?-6;
}
pNewWidth?=?pCodecContext->width;
pNewHeight?=?pCodecContext->height;
int?DisposeSize?=?avpicture_get_size(PIX_FMT_YUV420P?pCodecContext->width?pCodecContext->height);
uint8_t?*DisposeBuf?=?(uint8_t?*)av_malloc(DisposeSize*sizeof(uint8_t));
avpicture_fill((AVPicture?*)pframeDispose?DisposeBuf?PIX_FMT_YUV420P?pCodecContext->width?pCodecContext->height);
AVPacket?pPacket;
int?got_picture_ptr?=?0;
while?(av_read_frame(pFormatContext?&pPacket)?>=?0)
{
if?(pPacket.stream_index?==?0)
{
if?(avcodec_decode_video2(pCodecContext?pframe?&got_picture_ptr?&pPacket)?>=?0)
{
if?(got_picture_ptr)
{
SetVideoframe(pCodecContext?pframe?pframeDispose?pCodecContext->width?pCodecContext->height);
fwrite(DisposeBuf?1?DisposeSize?fw);
}
}
}
}
fclose(fw);
av_free(DisposeBuf);
av_frame_free(&pframeDispose);
av_frame_free(&pframe);
avcodec_close(pCodecContext);
avformat_close_input(&pFormatContext);
return?0;
}
int?yuv420p_to_mp4(char*?yuv420p_name?char*?mp4_name)
{
FILE*?fr_yuv420p?=?fopen(yuv420p_name?“rb“);
if?(fr_yuv420p?==?NULL)
return?-1;
AVOutputFormat*?pOutputForm
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-01-05?16:47??FFmpeg_avi_to_mp4\
?????目錄???????????0??2017-01-05?16:47??FFmpeg_avi_to_mp4\Debug\
?????文件???????40960??2017-01-05?16:45??FFmpeg_avi_to_mp4\Debug\FFmpeg.exe
?????文件??????290372??2017-01-05?16:45??FFmpeg_avi_to_mp4\Debug\FFmpeg.ilk
?????文件??????478208??2017-01-05?16:45??FFmpeg_avi_to_mp4\Debug\FFmpeg.pdb
?????目錄???????????0??2017-01-05?16:47??FFmpeg_avi_to_mp4\FFmpeg\
?????文件??????104448??2017-01-05?16:00??FFmpeg_avi_to_mp4\FFmpeg\20170105.avi
?????目錄???????????0??2017-01-05?16:47??FFmpeg_avi_to_mp4\FFmpeg\Debug\
?????文件?????????927??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.Build.CppClean.log
?????文件????????3554??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.log
?????文件???????23416??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.obj
?????目錄???????????0??2017-01-05?16:47??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\
?????文件????????8330??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\CL.read.1.tlog
?????文件?????????548??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\CL.write.1.tlog
?????文件?????????178??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\FFmpeg.lastbuildstate
?????文件?????????682??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\cl.command.1.tlog
?????文件????????1730??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\li
?????文件????????3892??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\li
?????文件?????????522??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\FFmpeg.tlog\li
?????文件??????150528??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\vc120.idb
?????文件??????135168??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\Debug\vc120.pdb
?????文件????????7804??2017-01-05?16:45??FFmpeg_avi_to_mp4\FFmpeg\FFmpeg.cpp
?????文件????????4482??2017-01-05?15:48??FFmpeg_avi_to_mp4\FFmpeg\FFmpeg.vcxproj
?????文件?????????955??2015-09-06?11:08??FFmpeg_avi_to_mp4\FFmpeg\FFmpeg.vcxproj.filters
?????文件?????????143??2015-09-06?11:06??FFmpeg_avi_to_mp4\FFmpeg\FFmpeg.vcxproj.user
?????文件????22361088??2015-06-05?10:06??FFmpeg_avi_to_mp4\FFmpeg\avcodec-56.dll
?????文件?????1380352??2015-06-05?10:06??FFmpeg_avi_to_mp4\FFmpeg\avdevice-56.dll
?????文件?????2393600??2015-06-05?10:06??FFmpeg_avi_to_mp4\FFmpeg\avfilter-5.dll
?????文件?????6006784??2015-06-05?10:06??FFmpeg_avi_to_mp4\FFmpeg\avformat-56.dll
?????文件??????493568??2015-06-05?10:06??FFmpeg_avi_to_mp4\FFmpeg\avutil-54.dll
?????目錄???????????0??2017-01-05?16:47??FFmpeg_avi_to_mp4\FFmpeg\include\
............此處省略123個文件信息
評論
共有 條評論