gif,即动态图,只带视频,不带声音,主要原因是声音太敏感,自动播放不得了。
pix_fmt是rgb的,不会是yuv,主要是计算机在显示图像时,是按着rgb的方式进行显示;所以gif的pix_fmt是rgb,就减少了一次yuv到rgb的转换,使得播放效果流畅。
本文读取一个mp4里面的内容,读出来是yuv420p格式,然后转成AV_PIX_FMT_BGR8,进行编码,产生最终的gif文件。
其中转换器构建的代码如下:
m_pImgConvertCtx = sws_getContext(m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, (AVPixelFormat)m_pFormatCtx_FileA->streams[0]->codecpar->format,m_iVideoWidth, m_iVideoHeight, m_pCodecEncodeCtx_Video->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
转换过程代码如下:
int iScale = sws_scale(m_pImgConvertCtx, (const uint8_t* const*)pFrameVideoA->data, pFrameVideoA->linesize, 0, iOutVideoHeight, pFrameGif->data, pFrameGif->linesize);
代码工程结构如下:
其中FfmpegGifTest.cpp的代码如下:
#include <iostream>
#include <vector>#include "ConvertToGif.h"#ifdef __cplusplus
extern "C"
{
#endif#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
};
#endifint main()
{CConvertToGif cConvertToGif;const char *pFileA = "E:\\learn\\ffmpeg\\FfmpegGifTest\\x64\\Release\\huoluan3.mp4";const char *pFileOut = "E:\\learn\\ffmpeg\\FfmpegGifTest\\x64\\Release\\huoluan3.gif";cConvertToGif.StartConvert(pFileA, pFileOut);cConvertToGif.WaitFinish();return 0;
}
ConvertToGif.h的代码如下:
#pragma once#include <Windows.h>#ifdef __cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavdevice/avdevice.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avutil.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"#ifdef __cplusplus
};
#endifclass CConvertToGif
{
public:CConvertToGif();~CConvertToGif();
public:int StartConvert(const char *pFileA, const char *pFileOut);int WaitFinish();
private:int OpenFileA(const char *pFileA);int OpenOutPut(const char *pFileOut);
private:static DWORD WINAPI VideoAReadProc(LPVOID lpParam);void VideoARead();static DWORD WINAPI VideoConvertProc(LPVOID lpParam);void VideoConvert();
private:AVFormatContext *m_pFormatCtx_FileA = NULL;AVCodecContext *m_pReadCodecCtx_VideoA = NULL;AVCodec *m_pReadCodec_VideoA = NULL;AVCodecContext *m_pCodecEncodeCtx_Video = NULL;AVFormatContext *m_pFormatCtx_Out = NULL;AVFifoBuffer *m_pVideoAFifo = NULL;int m_iVideoWidth = 1920;int m_iVideoHeight = 1080;int m_iYuv420FrameSize = 0;
private:CRITICAL_SECTION m_csVideoASection;HANDLE m_hVideoAReadThread = NULL;HANDLE m_hVideoConvertThread = NULL;AVRational m_streamTimeBase;SwsContext *m_pImgConvertCtx = NULL;
};
ConvertToGif.cpp的代码如下:
#include "ConvertToGif.h"CConvertToGif::CConvertToGif()
{InitializeCriticalSection(&m_csVideoASection);
}CConvertToGif::~CConvertToGif()
{DeleteCriticalSection(&m_csVideoASection);
}int CConvertToGif::StartConvert(const char *pFileA, const char *pFileOut)
{int ret = -1;do{ret = OpenFileA(pFileA);if (ret != 0){break;}ret = OpenOutPut(pFileOut);if (ret != 0){break;}m_pImgConvertCtx = sws_getContext(m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, (AVPixelFormat)m_pFormatCtx_FileA->streams[0]->codecpar->format,m_iVideoWidth, m_iVideoHeight, m_pCodecEncodeCtx_Video->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);m_iYuv420FrameSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, 1);//申请30帧缓存m_pVideoAFifo = av_fifo_alloc(30 * m_iYuv420FrameSize);m_hVideoAReadThread = CreateThread(NULL, 0, VideoAReadProc, this, 0, NULL);m_hVideoConvertThread = CreateThread(NULL, 0, VideoConvertProc, this, 0, NULL);} while (0);return ret;
}int CConvertToGif::WaitFinish()
{int ret = 0;do{if (NULL == m_hVideoAReadThread){break;}WaitForSingleObject(m_hVideoAReadThread, INFINITE);CloseHandle(m_hVideoAReadThread);m_hVideoAReadThread = NULL;WaitForSingleObject(m_hVideoConvertThread, INFINITE);CloseHandle(m_hVideoConvertThread);m_hVideoConvertThread = NULL;} while (0);return ret;
}int CConvertToGif::OpenFileA(const char *pFileA)
{int ret = -1;do{if ((ret = avformat_open_input(&m_pFormatCtx_FileA, pFileA, 0, 0)) < 0) {printf("Could not open input file.");break;}if ((ret = avformat_find_stream_info(m_pFormatCtx_FileA, 0)) < 0) {printf("Failed to retrieve input stream information");break;}if (m_pFormatCtx_FileA->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO){break;}m_streamTimeBase = m_pFormatCtx_FileA->streams[0]->time_base;m_pReadCodec_VideoA = (AVCodec *)avcodec_find_decoder(m_pFormatCtx_FileA->streams[0]->codecpar->codec_id);m_pReadCodecCtx_VideoA = avcodec_alloc_context3(m_pReadCodec_VideoA);if (m_pReadCodecCtx_VideoA == NULL){break;}avcodec_parameters_to_context(m_pReadCodecCtx_VideoA, m_pFormatCtx_FileA->streams[0]->codecpar);m_iVideoWidth = m_pReadCodecCtx_VideoA->width;m_iVideoHeight = m_pReadCodecCtx_VideoA->height;m_pReadCodecCtx_VideoA->framerate = m_pFormatCtx_FileA->streams[0]->r_frame_rate;if (avcodec_open2(m_pReadCodecCtx_VideoA, m_pReadCodec_VideoA, NULL) < 0){break;}ret = 0;} while (0);return ret;
}int CConvertToGif::OpenOutPut(const char *pFileOut)
{int iRet = -1;AVStream *pAudioStream = NULL;AVStream *pVideoStream = NULL;do{avformat_alloc_output_context2(&m_pFormatCtx_Out, NULL, NULL, pFileOut);{AVCodec* pCodecEncode_Video = (AVCodec *)avcodec_find_encoder(m_pFormatCtx_Out->oformat->video_codec);m_pCodecEncodeCtx_Video = avcodec_alloc_context3(pCodecEncode_Video);if (!m_pCodecEncodeCtx_Video){break;}pVideoStream = avformat_new_stream(m_pFormatCtx_Out, pCodecEncode_Video);if (!pVideoStream){break;}int frameRate = m_pFormatCtx_FileA->streams[0]->r_frame_rate.num / m_pFormatCtx_FileA->streams[0]->r_frame_rate.den;m_pCodecEncodeCtx_Video->flags |= AV_CODEC_FLAG_QSCALE;m_pCodecEncodeCtx_Video->bit_rate = 4000000;m_pCodecEncodeCtx_Video->rc_min_rate = 4000000;m_pCodecEncodeCtx_Video->rc_max_rate = 4000000;m_pCodecEncodeCtx_Video->bit_rate_tolerance = 4000000;m_pCodecEncodeCtx_Video->time_base.den = frameRate;m_pCodecEncodeCtx_Video->time_base.num = 1;m_pCodecEncodeCtx_Video->width = m_iVideoWidth;m_pCodecEncodeCtx_Video->height = m_iVideoHeight;//pH264Encoder->pCodecCtx->frame_number = 1;m_pCodecEncodeCtx_Video->gop_size = 12;m_pCodecEncodeCtx_Video->max_b_frames = 0;m_pCodecEncodeCtx_Video->thread_count = 4;m_pCodecEncodeCtx_Video->pix_fmt = AV_PIX_FMT_BGR8;m_pCodecEncodeCtx_Video->codec_type = AVMEDIA_TYPE_VIDEO;av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "b-pyramid", "none", 0);av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "preset", "superfast", 0);av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "tune", "zerolatency", 0);if (m_pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)m_pCodecEncodeCtx_Video->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;if (avcodec_open2(m_pCodecEncodeCtx_Video, pCodecEncode_Video, 0) < 0){//编码器打开失败,退出程序break;}}if (!(m_pFormatCtx_Out->oformat->flags & AVFMT_NOFILE)){if (avio_open(&m_pFormatCtx_Out->pb, pFileOut, AVIO_FLAG_WRITE) < 0){break;}}avcodec_parameters_from_context(pVideoStream->codecpar, m_pCodecEncodeCtx_Video);if (avformat_write_header(m_pFormatCtx_Out, NULL) < 0){break;}iRet = 0;} while (0);if (iRet != 0){if (m_pCodecEncodeCtx_Video != NULL){avcodec_free_context(&m_pCodecEncodeCtx_Video);m_pCodecEncodeCtx_Video = NULL;}if (m_pFormatCtx_Out != NULL){avformat_free_context(m_pFormatCtx_Out);m_pFormatCtx_Out = NULL;}}return iRet;
}DWORD WINAPI CConvertToGif::VideoAReadProc(LPVOID lpParam)
{CConvertToGif *pConvertToGif = (CConvertToGif *)lpParam;if (pConvertToGif != NULL){pConvertToGif->VideoARead();}return 0;
}void CConvertToGif::VideoARead()
{AVFrame *pFrame;pFrame = av_frame_alloc();int y_size = m_pReadCodecCtx_VideoA->width * m_pReadCodecCtx_VideoA->height;char *pY = new char[y_size];char *pU = new char[y_size / 4];char *pV = new char[y_size / 4];AVPacket packet = { 0 };int ret = 0;while (1){av_packet_unref(&packet);ret = av_read_frame(m_pFormatCtx_FileA, &packet);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0){break;}if (packet.stream_index != 0){continue;}ret = avcodec_send_packet(m_pReadCodecCtx_VideoA, &packet);if (ret >= 0){ret = avcodec_receive_frame(m_pReadCodecCtx_VideoA, pFrame);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0) {break;}while (1){if (av_fifo_space(m_pVideoAFifo) >= m_iYuv420FrameSize){///Yint contY = 0;for (int i = 0; i < pFrame->height; i++){memcpy(pY + contY, pFrame->data[0] + i * pFrame->linesize[0], pFrame->width);contY += pFrame->width;}///Uint contU = 0;for (int i = 0; i < pFrame->height / 2; i++){memcpy(pU + contU, pFrame->data[1] + i * pFrame->linesize[1], pFrame->width / 2);contU += pFrame->width / 2;}///Vint contV = 0;for (int i = 0; i < pFrame->height / 2; i++){memcpy(pV + contV, pFrame->data[2] + i * pFrame->linesize[2], pFrame->width / 2);contV += pFrame->width / 2;}EnterCriticalSection(&m_csVideoASection);av_fifo_generic_write(m_pVideoAFifo, pY, y_size, NULL);av_fifo_generic_write(m_pVideoAFifo, pU, y_size / 4, NULL);av_fifo_generic_write(m_pVideoAFifo, pV, y_size / 4, NULL);LeaveCriticalSection(&m_csVideoASection);break;}else{Sleep(100);}}}if (ret == AVERROR(EAGAIN)){continue;}}av_frame_free(&pFrame);delete[] pY;delete[] pU;delete[] pV;
}DWORD WINAPI CConvertToGif::VideoConvertProc(LPVOID lpParam)
{CConvertToGif *pVideoConvert = (CConvertToGif *)lpParam;if (pVideoConvert != NULL){pVideoConvert->VideoConvert();}return 0;
}void CConvertToGif::VideoConvert()
{int ret = 0;int iYuv420PadFrameSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, m_iVideoWidth, m_iVideoHeight, 1);int y_size = m_iVideoWidth * m_iVideoHeight;DWORD dwBeginTime = ::GetTickCount();AVFrame *pFrameVideoA = av_frame_alloc();uint8_t *videoA_buffer_yuv420 = (uint8_t *)av_malloc(m_iYuv420FrameSize);av_image_fill_arrays(pFrameVideoA->data, pFrameVideoA->linesize, videoA_buffer_yuv420, AV_PIX_FMT_YUV420P, m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, 1);pFrameVideoA->width = m_iVideoWidth;pFrameVideoA->height = m_iVideoHeight;pFrameVideoA->format = AV_PIX_FMT_YUV420P;int iOutVideoWidth = m_pReadCodecCtx_VideoA->width;int iOutVideoHeight = m_pReadCodecCtx_VideoA->height;AVFrame *pFrameGif = av_frame_alloc();int frame_size = av_image_get_buffer_size(m_pCodecEncodeCtx_Video->pix_fmt, m_iVideoWidth, m_iVideoHeight, 1);uint8_t *out_buffer_bgr8 = (uint8_t *)av_malloc(frame_size);av_image_fill_arrays(pFrameGif->data, pFrameGif->linesize, out_buffer_bgr8, m_pCodecEncodeCtx_Video->pix_fmt, iOutVideoWidth, iOutVideoHeight, 1);pFrameGif->width = m_iVideoWidth;pFrameGif->height = m_iVideoHeight;pFrameGif->format = m_pCodecEncodeCtx_Video->pix_fmt;AVPacket packet = { 0 };int iPicCount = 0;while (1){if (NULL == m_pVideoAFifo){break;}int iVideoASize = av_fifo_size(m_pVideoAFifo);if (iVideoASize >= m_iYuv420FrameSize){EnterCriticalSection(&m_csVideoASection);av_fifo_generic_read(m_pVideoAFifo, videoA_buffer_yuv420, m_iYuv420FrameSize, NULL);LeaveCriticalSection(&m_csVideoASection);int iScale = sws_scale(m_pImgConvertCtx, (const uint8_t* const*)pFrameVideoA->data, pFrameVideoA->linesize, 0, iOutVideoHeight, pFrameGif->data, pFrameGif->linesize);pFrameGif->pkt_dts = pFrameGif->pts = av_rescale_q_rnd(iPicCount, m_pCodecEncodeCtx_Video->time_base, m_pFormatCtx_Out->streams[0]->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));pFrameGif->pkt_duration = 0;pFrameGif->pkt_pos = -1;ret = avcodec_send_frame(m_pCodecEncodeCtx_Video, pFrameGif);ret = avcodec_receive_packet(m_pCodecEncodeCtx_Video, &packet);av_write_frame(m_pFormatCtx_Out, &packet);iPicCount++;}else{if (m_hVideoAReadThread == NULL){break;}Sleep(1);}}av_write_trailer(m_pFormatCtx_Out);avio_close(m_pFormatCtx_Out->pb);av_frame_free(&pFrameVideoA);
}