当前位置:网站首页>FFmpeg compiles and installs on win10 (configure libx264)
FFmpeg compiles and installs on win10 (configure libx264)
2022-08-09 13:04:00 【Know 12】
背景
使用带h264的ffmpeg才是我们的最终目的!
一、先把x264搞定(包含环境)
https://blog.csdn.net/qq_37249793/article/details/115272864
二、Windows配置步骤
1.下载FFmpeg
官网(http://www.ffmpeg.org/)下载源码
2.配置环境
- 重命名C:/msys64/usr/bin/link.exe 为C:/msys64/usr/bin/link.bak, 避免和MSVC 的link.exe抵触
- 下载yasm(http://yasm.tortall.net/Download.html)改名为yasm.exe放在/usr/bin/中
- 把msys目录下的msys2_shell.cmd的
rem set MSYS2_PATH_TYPE=inherit
去掉rem可以继承vs的编译环境 - 打开[适用于 VS 2017 的 x64 本机工具命令提示],输入
msys2_shell.cmd -mingw64
- Check if the environment is set up:
which cl link yasm cpp
- 把编译好的x264和解压的FFmpeg放在msys/home中,在FFpeg下新建build.sh:
./configure --toolchain=msvc --target-os=win64 \
--arch=x86_64 \
--enable-shared \
--enable-small \
--enable-version3 \
--enable-gpl \
--enable-nonfree \
--disable-stripping \
--disable-encoders \
--disable-decoders \
--enable-decoder=h264 \
--enable-encoder=libx264 \
--enable-encoder=mjpeg \
--enable-encoder=mpeg4 \
--prefix=./build \
--enable-libx264 \
--extra-cflags="-I/home/x264/include" \
--extra-ldflags="-LIBPATH:/home/x264/lib"
The bottom two lines arex264对应目录
7. 输入命令sh build.sh
,如果保WARNING: using libx264 without pkg-config可以不用管
8. 接下来就是编译了:
make
make install
- 生成后,基本同上一篇部署,如果缺东西的话,Also put alldll(包括libx264的)都放在Windows\System32里就可以了
3.测试
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
#include "libswscale/swscale.h"
};
#include <afxwin.h>
#pragma warning(disable:4996)
using namespace std;
typedef struct
{
int startpos;
int endpos;
int len;
int code;
int startlen;
}FrameAna;
unsigned char m_outbuf[1024 * 1024];
typedef struct
{
uint8_t m_Head[4];
int m_len;
}FrameHead_t;
void Parser();
int main()
{
Parser();
return 0;
}
//mp4中获取yuv
void Parser()
{
AVFormatContext *pFormatCtx;
int videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
uint8_t *out_buffer;
AVPacket *packet;
int ret, got_picture;
struct SwsContext *img_convert_ctx;
char filepath[] = "000.mp4";//要解析的mp4文件
FILE *fp_h264 = NULL;
FrameAna m_FrameInfo[1024];
int m_FrameIndex = 0;
fopen_s(&fp_h264, "000.h264", "wb+");//MP4输出成.h264文件
/* 初始化一个AVFormatContext ,并打开文件 */
pFormatCtx = avformat_alloc_context();
avformat_open_input(&pFormatCtx, filepath, NULL, NULL);
/* 获取视频文件信息 */
avformat_find_stream_info(pFormatCtx, NULL);
/* Find the video's number */
videoindex = -1;
for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoindex = (int)i;
break;
}
if (videoindex == -1)
{
AfxMessageBox(L"can't find video,error!");
return;
}
}
/* 查找解码器 */
pCodecCtx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoindex]->codecpar);
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
/* 打开解码器 */
avcodec_open2(pCodecCtx, pCodec, NULL);
pFrame = av_frame_alloc();
out_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
packet = (AVPacket*)av_malloc(sizeof(AVPacket));
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
/* Get the stream parsing object */
unsigned char *h264_buf;
int h264_len;
AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");
int iNeedIFrame = 1;
int iDecoderCnt = 0;
while (av_read_frame(pFormatCtx, packet) >= 0)
{
/* Read a frame of compressed data */
if (packet->stream_index == videoindex)
{
/* 数据解码,可获取到YUV解码后数据 */
ret = avcodec_send_packet(pCodecCtx, packet);
if (ret < 0)
{
AfxMessageBox(L"Decoder one frame , Error !");
return;
}
got_picture = avcodec_receive_frame(pCodecCtx, pFrame);
if (0 == got_picture)
{
iDecoderCnt++;
if (nullptr != fp_h264)
{
fwrite(pFrame->data[0], 1, pFrame->width * pFrame->height, fp_h264);
fwrite(pFrame->data[1], 1, pFrame->width * pFrame->height / 4, fp_h264);
fwrite(pFrame->data[2], 1, pFrame->width * pFrame->height / 4, fp_h264);
printf("I'm editing");
//Called every frametojpg函数转换为jpg图像
}
}
}
av_packet_unref(packet);
}
/* flush decoder */
/*当av_read_frame()循环退出的时候,In fact, the decoder may also contain the remaining few frames of data.因此需要通过“flush_decoder”Output these frames of data.“flush_decoder”The function is simply called directlyavcodec_decode_video2()获得AVFrame,instead of passing it to the decoderAVPacket.*/
while (1)
{
got_picture = avcodec_receive_frame(pCodecCtx, pFrame);
if (0 != got_picture)
break;
iDecoderCnt++;
}
sws_freeContext(img_convert_ctx);
/* Release the stream parsing object */
av_bitstream_filter_close(h264bsfc);
/* Close the file and free memory */
fclose(fp_h264);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
CString m_str;
m_str.Format(L"Get FrameCnt = %d Success !!\n", iDecoderCnt);
AfxMessageBox(m_str);
return;
}
边栏推荐
- 正则表达式(规则,匹配,和实际使用)
- proto3-2 syntax
- Ways to prevent data fraud
- Golang学习之路(五):Golang的函数
- How should the acceptance criteria for R&D requirements be written?| Agile Practices
- Senior told me that the giant MySQL is through SSH connection
- 国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
- 曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
- MySQL查询性能优化七种武器之索引潜水
- 00后写个暑假作业,被监控成这笔样
猜你喜欢
阿里高工带来的20022最新面试总结太香了
《数字经济全景白皮书》银行业智能营销应用专题分析 发布
【微服务~远程调用】整合RestTemplate、WebClient、Feign
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
GRPC整体学习
2022牛客多校(六)M. Z-Game on grid
Reading and writing after separation, performance were up 100%
用 API Factory 产品生成 API 文档
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
研发需求的验收标准应该怎么写? | 敏捷实践
随机推荐
Golang学习之路(五):Golang的函数
西湖大学教授怎么看AI制药革命?|量子位智库圆桌实录
人体解析(Human Parse)开源数据集整理
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
MongoDB-查询中$all的用法介绍
FFmpeg库在win10上配置使用(不配置libx264)
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
学长告诉我,大厂MySQL都是通过SSH连接的
腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
【面试高频题】可逐步优化的链表高频题
微服务架构的核心关键点
The redis library cannot be imported
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
香港服务器如何进行加密?
李开复花上千万投的缝纫机器人,团队出自大疆
WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?
electron 应用开发优秀实践
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
世界第4疯狂的科学家,在103岁生日那天去世了