Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active September 15, 2020 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yano3nora/b9caa9baa196ec6559e93a24addf4be5 to your computer and use it in GitHub Desktop.
Save yano3nora/b9caa9baa196ec6559e93a24addf4be5 to your computer and use it in GitHub Desktop.
[linux: FFmpeg] FFmpeg - Multimedia edit & convert tool. #linux #ffmpeg

References

How to use ?

# エンコード : ffmpeg -i 元動画ファイル [options] 出力動画ファイル
$ ffmpeg -i exapmle.mov -r 10 exapmle.mp4  # フレームレート (fps) 指定
$ ffmpeg -i exapmle.avi -s 1920x1080 exapmle.mp4  # サイズ指定
# 動画情報の取得 : ffprobe 動画ファイル [options]
$ ffprobe example.mov -show_streams  # 詳細情報の表示
$ ffprobe example.mp4 -print_format json  # JSON 出力

TIPS

元のファイルサイズを保ったまま再エンコード

ニッチ過ぎる。

$ ffmpeg -i source.mp4 -vcodec copy -acodec copy distination.avi

.mov を .gif にしたいよ!

$ ffmpeg -i "MOVIE.mov" -r 24 -vf scale=1280:-1 capture_1.gif

width / height not devising by 2 !!

-vf scale=640:-1 とかでアスペクト比保ちつつ自動計算...みたいにしようと思ったら、動画サイズ縦が 2 で割り切れないって怒られた。以下で丸めを挟む。

$ ffmpeg -threads 0 -y -i origin.mov -f mp4 -vf "scale=640:trunc(ow/a/2)*2" -b:v 1000k dest.mp4

シェルスクリプトから ffmpeg 走らないよ !!

多分ビルドしたユーザ以外への全体パスが通ってない。シンボリックリンクで全ユーザが $ ffmpeg -version でパスが通るようにしてやる。

$ sudo ln -s /usr/local/bin/ffmpeg /usr/bin/ffmpeg

幅指定してアスペクト比を自動算出

動画のアスペクト比を保ったまま、特定の幅に統一したい

動画サイズ・アスペクト比がまちまち ( 16:9 / 4:3 ) な動画について、出力時の横幅だけ固定し縦を自動計算する。

# 横 320px になりつつ、縦は元動画のアスペクト比から自動算出される
ffmpeg -y -i sample.mp4 -vf scale=320:-1  sample.out.mp4

Install

Vagrant で立てた CentOS 6.7 にインストール。

Refs

Command line

# コーデック入れる
$ sudo cd /var/tmp
$ sudo wget http://www8.mplayerhq.hu/MPlayer/releases/codecs/all-20110131.tar.bz2
$ sudo bunzip2 all-20110131.tar.bz2; tar xvf all-20110131.tar
$ sudo mkdir /usr/local/lib/codecs/
$ sudo mkdir /usr/local/lib64/codecs/
$ sudo cp all-20110131/* /usr/local/lib/codecs/
$ sudo cp all-20110131/* /usr/local/lib64/codecs/
$ sudo chmod -R 755 /usr/local/lib/codecs/
$ sudo chmod -R 755 /usr/local/lib64/codecs/

# epel リポジトリと git と入ってる前提
$ sudo yum -y install ImageMagick ImageMagick-devel
$ sudo yum -y install autoconf automake make gcc gcc-c++ pkgconfig wget libtool zlib-devel
$ sudo yum -y install --enablerepo=epel yasm
$ cd /usr/local/src
$ sudo git clone git://git.videolan.org/x264
$ cd x264
$ sudo ./configure --enable-shared --disable-asm
$ sudo make
$ sudo make install
$ cd /usr/local/src
$ sudo git clone --depth 1 git://github.com/mstorsjo/fdk-aac.git
$ cd fdk-aac
$ sudo autoreconf -fiv
$ sudo ./configure
$ sudo make
$ sudo make install
$ sudo export LD_LIBRARY_PATH=/usr/local/lib/
$ sudo sh -c "echo /usr/local/lib > /etc/ld.so.conf.d/custom-libs.conf"
$ sudo ldconfig
$ cd /usr/local/src
$ sudo git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
$ cd ffmpeg
$ sudo ./configure --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libx264 --enable-shared --arch=x86_64 --enable-pthreads
$ sudo make  # 鬼のように時間かかる
$ sudo make install
$ sudo ldconfig

# パス通し & 動作確認
$ sudo ln -s /usr/local/bin/ffmpeg /usr/bin/ffmpeg
$ ffmpeg -version

# フォーマット確認
$ ffmpeg -formats | less 
# File format - D:readable, E:writable
# Codecs - D: decodable, E:encodable (V:video, A:audio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment