ffmpeg mp4转gif

news/2024/10/30 16:21:46/

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);
}

http://www.ppmy.cn/news/759355.html

相关文章

用ffmpeg将视频转成gif动图

今天分享一个我制作表情包的技巧。现在视频编辑的门槛已经非常低了&#xff0c;只要装个剪映稍微学一下&#xff0c;很容易就能把你想要的内容剪出来&#xff0c;真的是有手就行。但是视频剪出来的视频是无法直接用做表情包的&#xff0c;只有gif格式的动图才是真正可以用的表情…

ffmpeg:gif转mov

gif转mov 缘由像素图缩放gif转视频调整坐标画面 翻转画面 旋转处理时长gif同框在ae中的实现结语 有些网站可以将gif转成mp4和mov&#xff0c;不用安装额外的软件&#xff0c;windows和mac上都能操作&#xff0c;操作便捷&#xff0c;使用方便&#xff0c;如无特殊要求&#xff…

GIF转MP4 - 在线将GIF动态图转为MP4视频文件

如何把 GIF 转成视频&#xff1f;一刀工具箱提供几秒钟内将 GIF 转换为 MP4 文件的最佳方法&#xff0c;在线将 GIF 动态图转为 MP4 视频文件&#xff0c;100% 免费、安全并便于操作&#xff01; 代码片段 chooseImage() {let that this;uni.chooseImage({count:1,sourceTyp…

mp4转换成gif怎么转?

mp4转gif&#xff0c;mp4转换成gif怎么转&#xff1f;mp4是最重要也是最常见的视频文件格式&#xff0c;而gif是动态图片的主要格式&#xff0c;有时候因为某些原因&#xff0c;在工作或者学习中需要将mp4视频转换成gif动图&#xff0c;那么你知道从何下手进行转换操作吗&#…

将GIF转成视频MP4、MOV

将GIF转成视频MP4、MOV 无需下载其他视频转换软件&#xff0c;仅借助PowerPoint实现 GIF格式动图 转换为 MP4 或者 MOV格式视频。 Bilibili拾叁学长 https://www.bilibili.com/video/BV1CD4y1o7pv?fromsearch&seid6391893059452592042步骤&#xff1a; 新建PowerPoint文…

gen1-视频生成论文阅读

文章目录 摘要贡献算法3.1 LDM3.2 时空隐空间扩散3.3表征内容及结构内容表征结构表征条件机制采样 3.4优化过程 实验结果结论 论文&#xff1a; 《Structure and Content-Guided Video Synthesis with Diffusion Models》 官网&#xff1a; https://research.runwayml.com/ge…

[日常]mov文件转换为gif

因为需要将之前mac下用QuickTime录屏生成的文件(mov格式)转换成gif文件, 便于传到某些博客平台, 于是找到了这个转换工具, 已将原代码的命名中文化并简化. Ruby和视频转换都是新手, 请多指教. 之前JavaScript实现ZLOGO: 前进方向和速度有两个mov文件, 下面为转换出的gif: “轮…

视频文件转gif图片Movie To GIF使用

系列文章目录 前言 在写博客的时候&#xff0c;如果你想把视频文件上传到网页上怎么办&#xff0c;可能有些网页只支持gif文件格式上传&#xff0c;无法直接上传wmv、MP4 等视频格式&#xff0c;怎么办。百度上搜索的视频转gif格式工具&#xff0c;要么是要收费的&#xff0c…