Skip to content

Instantly share code, notes, and snippets.

@zxhfighter
Last active March 9, 2022 03:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zxhfighter/6b212416245f86d01b85984d5d3b69e1 to your computer and use it in GitHub Desktop.
Save zxhfighter/6b212416245f86d01b85984d5d3b69e1 to your computer and use it in GitHub Desktop.
mac 安装 ffmpeg

直接安装

brew install ffmpeg

如果需要安装第三方 options,可以如下操作:

1.执行

brew tap homebrew-ffmpeg/ffmpeg

2.执行

brew install homebrew-ffmpeg/ffmpeg/ffmpeg

3.执行如下命令,看此仓库支持哪些options。

brew options homebrew-ffmpeg/ffmpeg/ffmpeg

4.根据第3)步得到的options,替换下面命令中的option,然后执行

brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-<option1> --with-<option2> ...

手动编译安装

  1. 新建下载目录
mkdir ffm && cd $_
  1. 下载编译源代码
git clone https://git.ffmpeg.org/ffmpeg.git
  1. 编译

这个步骤检查配置、依赖是否满足,注意 --prefix 参数为从 git 下载后的 ffmpeg 目录。

./configure --prefix=/Users/zxh/ffm/ffmpeg  --enable-gpl  --enable-nonfree  --enable-libfdk-aac  --enable-libx264  --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --cc=clang --host-cflags= --host-ldflags=

当中可能会有很多包找不到,逐个使用 brew install 安装即可。

  • yasm 找不到:brew install yasm
  • fdk-aac 找不到:brew install fdk-aac
  • speex 找不到:brew install speex
  • pkg-config 找不到:brew install pkg-config
  • libx264 找不到:brew install x264
  • libx265 找不到:brew install x265
  1. 安装

这个步骤进行具体的安装。

make && make install

如果没有权限,使用 sudo

使用技巧

一次性下载多个文件

使用 shell 扩展运算符 {num1..num2} 结合 wget 命令一起使用。

wget https://www.abc.com/{1..40}.mp4

使用 ffmpeg 合并多个视频

首先使用 for 循环将所有 mp4 转化为 .ts 文件,先将 mp4 转化为同样编码形式的 ts 流,因为 ts 流是可以 concate 的,先把 mp4 封装成 ts ,然后 concate ts 流, 最后再把 ts 流转化为 mp4。

for i in {1..40}; do ffmpeg -i $i.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb $i.ts; done;

然后新建一个输入清单文件。

> list.txt

然后使用 for 循环生成 file 1.ts 描述文件。

for i in {1..40}; do echo "file $i.ts" >> list.txt; done;

最后使用合并命令合并。

ffmpeg -f concat -i list.txt -acodec copy -vcodec copy -absf aac_adtstoasc output.mp4

剪切视频

使用 -ss 和 -t 选项,从第0秒开始,向后截取31秒视频,并保存.

ffmpeg -ss 00:00:00 -i video.mp4 -vcodec copy -acodec copy -t 00:00:31 output1.mp4

从第01:33:30 开始,向后截取 00:47:16 视频,并保存

ffmpeg -ss 01:33:30 -i video.mp4 -vcodec copy -acodec copy -t 00:47:16 output2.mp4

抽帧

抽帧,每20秒抽一帧

常见命令

// 去掉视频中的音频
ffmpeg -i input.mp4 -vcodec copy -an output.mp4
// -an: 去掉音频;-vcodec:视频选项,一般后面加copy表示拷贝

// 提取视频中的音频
ffmpeg -i input.mp4 -acodec copy -vn output.mp3
// -vn: 去掉视频;-acodec: 音频选项, 一般后面加copy表示拷贝
// 如果提示: Invalid audio stream. Exactly one MP3 audio stream is required.
// 那么需要查看视频的音频编码,在上边文件简介中可以找到,例如
// Duration: 00:01:37.96, start: 0.000000, bitrate: 750 kb/s
//    Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 48 kb/s (default)
// 因此上边需要输入 output.aac

// 音视频合成
ffmpeg -y –i input.mp4 –i input.mp3 –vcodec copy –acodec copy output.mp4
// -y 覆盖输出文件

//剪切视频
ffmpeg -ss 0:1:30 -t 0:0:20 -i input.mp4 -vcodec copy -acodec copy output.mp4
// -ss 开始时间; -t 持续时间

// 视频截图
ffmpeg –i test.mp4 –f image2 -t 0.001 -s 320x240 image-%3d.jpg
// -s 设置分辨率; -f 强迫采用格式fmt;

// 视频分解为图片
ffmpeg –i test.mp4 –r 1 –f image2 image-%3d.jpg
// -r 指定截屏频率

// 将图片合成视频
ffmpeg -f image2 -i image%d.jpg output.mp4

//视频拼接
ffmpeg -f concat -i filelist.txt -c copy output.mp4

// 将视频转为gif
ffmpeg -i input.mp4 -ss 0:0:30 -t 10 -s 320x240 -pix_fmt rgb24 output.gif
// -pix_fmt 指定编码

// 将视频前30帧转为gif
ffmpeg -i input.mp4 -vframes 30 -f gif output.gif

// 旋转视频
ffmpeg -i input.mp4 -vf rotate=PI/2 output.mp4

// 缩放视频
ffmpeg -i input.mp4 -vf scale=iw/2:-1 output.mp4
// iw 是输入的宽度, iw/2就是一半;-1 为保持宽高比

//视频变速
ffmpeg -i input.mp4 -filter:v setpts=0.5*PTS output.mp4

//音频变速
ffmpeg -i input.mp3 -filter:a atempo=2.0 output.mp3

//音视频同时变速,但是音视频为互倒关系
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mp4


// 视频添加水印
ffmpeg -i input.mp4 -i logo.jpg -filter_complex [0:v][1:v]overlay=main_w-overlay_w-10:main_h-overlay_h-10[out] -map [out] -map 0:a -codec:a copy output.mp4
// main_w-overlay_w-10 视频的宽度-水印的宽度-水印边距;

// 截取视频局部
ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4
// 截取部分视频,从[80,60]的位置开始,截取宽200,高100的视频
ffmpeg -i in.mp4 -filter:v "crop=80:60:200:100" -c:a copy out.mp4
// 截取右下角的四分之一
ffmpeg -i in.mp4 -filter:v "crop=in_w/2:in_h/2:in_w/2:in_h/2" -c:a copy out.mp4
// 截去底部40像素高度
ffmpeg -i in.mp4 -filter:v "crop=in_w:in_h-40" -c:a copy out.mp4

参数说明:

参数说明:

-vcodec xvid 使用xvid压缩 -s 320x240 指定分辨率 -r fps 设置帧频 缺省25 -b <比特率> 指定压缩比特

-acodec aac 设定声音编码 -ac <数值> 设定声道数,1就是单声道,2就是立体声 -ar <采样率> 设定声音采样率,PSP只认24000 -ab <比特率> 设定声音比特率 -vol <百分比> 设定音量

-y(覆盖输出文件

-t duration 设置纪录时间 hh:mm:ss[.xxx]格式的记录时间也支持 -ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持 -title string 设置标题 -author string 设置作者 -copyright string 设置版权 -hq 激活高质量设置

-aspect aspect 设置横纵比 4:3 16:9 或 1.3333 1.7777 -croptop size 设置顶部切除带大小 像素单位 -cropbottom size -cropleft size -cropright size -padtop size 设置顶部补齐的大小 像素单位 -padbottom size -padleft size -padright size -padcolor color 设置补齐条颜色(hex,6个16进制的数,红:绿:兰排列,比如 000000代表黑色) -bt tolerance 设置视频码率容忍度kbit/s -maxrate bitrate设置最大视频码率容忍度 -minrate bitreate 设置最小视频码率容忍度 -bufsize size 设置码率控制缓冲区大小 -vcodec codec 强制使用codec编解码方式。 如果用copy表示原始编解码数据必须被拷贝。 -sameq 使用同样视频质量作为源(VBR) -pass n 选择处理遍数(1或者2)。两遍编码非常有用。第一遍生成统计信息,第二遍生成精确的请求的码率 -passlogfile file 选择两遍的纪录文件名为file

-map file:stream 设置输入流映射 -debug 打印特定调试信息

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment