Nodejs将h264/h265转码成mp4或某一帧图片
前置知识
-
认识FFmpeg
-
安装ffmpeg
- Linux
yum install -y ffmpeg
- Mac
- 下载地址 地址
- Linux
-
指令范例
- 将h264转换为mp4
ffmpeg -i test.h264 -c:v libx264 -strict -2 test.mp4
- 将mp4按指定帧率切割成图片
ffmpeg -i test.mp4 -r 指定帧率 -f image2 test/image-%03d.jpg
- 对于24帧每秒的视频 128帧是第5秒的第四帧
ffmpeg -i test.h264 -threads 1 -ss 00:00:05.167 -f image2 -r 1 -t 1 -s 256*256 test-%2d.jpg
- 要在1秒到20秒允许一个blur(模糊)滤镜(smartblur),然后curves滤镜在3秒之后
ffplay -i good_scale.mp4 -vf "smartblur=enable='between(t,1,20)',curves=enable='gte(t,3)':preset=cross_process"
- 将h462的第一帧保存为图片
ffmpeg -i test.h264 -vf "select=between(n\,0\,1)" -y test-%2d.jpg
- 将h264转换为mp4
技术栈
- Nodejs、Express、FFmpeg
实现方案
核心代码(这一步就是使用nodeshell执行ffmpeg指令,后续自己按照业务去扩展)
const shell = require('shelljs');
// 执行ffmpeg
shell.exec(`ffmpeg -i topic.h264 -vf "select='between(n,0,1)'" -y -acodec copy topic.jpg`, (error, stdout, stderr) => {});
更多拓展(可以忽略)
- 使用JavaScript播放H264视频 地址
文章转自 https://elasticnotes.com/node/h264/
complete!!