ffmpeg Field Notes

with tags media ffmpeg field-notes -

This is a conversation of an old DokuWiki post where I would just dump ffmpeg commands for my reference. It really needs to be split into multiple pages, but might be useful to someone in the raw form.

Links

Oneliners

# File Info
ffmpeg -i <file>

# Convert (not transcode)
ffmpeg -i <file> -c copy <file.out>

# Convert lowbitrate -> 128k (to get around iCloud)
ffmpeg -i in.m4a -b:a 128k out.mp3

# Convert, but transcode audio to 2ch AC3
ffmpeg -i <file.mkv> -c:v copy -c:a ac3 -ac 2 <file.mp4>

# Extract Audio Stream (0:1 = 0-First input file  1-Second Stream)
ffmpeg -i Clue.m4v -map 0:1 -acodec copy Clue_Audio.m4a

# Extract Audio Stream (0:1 = 0-First input file  1-Second Stream)
#   Copy 5 minutes from 25 min in (1500s=25m, 300s=5m)
ffmpeg -i Clue.m4v -map 0:1 -acodec copy -ss 1500 -t 300 Clue_Audio.m4a

# Convert all MP3 files to AAC, then rename to M4B (for audiobooks)
for f in *.mp3; do
  ffmpeg -i $f -c:a libfdk_aac ${f:r}.m4a && \
  mv ${f:r}.m4a ${f:r}.m4b
done
for f in *.mp3; do ffmpeg -i $f -f adts -c:a libfdk_aac ${f:r}.m4b; done
for f in *.mp3; do
  ffmpeg -i $f -acodec libfdk_aac -b:a 96k -f adts ${f:r}.m4b
done

# Convert to AAC/M4A that will import into iTunes
ffmpeg -i Input.mp3 -c:a libfdk_aac Test96.m4a
#     If you want a different file extension, specify iPod format:
ffmpeg -i Input.mp3 -c:a libfdk_aac -f ipod -b:a 96k Test96.m4b

# Convert all FLAC->M4A, using separate folders
mkdir aac; mkdir processed; for f in *.flac; do
  ffmpeg -i $f -acodec libfdk_aac -b:a 256k "aac/${f:t:r}.m4a" && \
  mv $f processed/
done
# Simple FLAC conversion
for f in *.flac; do
  ffmpeg -i $f -c:a libfdk_aac -ab 256k ${f:r}.m4a
done

# Take an input that has 0:0-h264 0:1-ac3 and output a file that copies those and adds an AAC channel
# Notice the duplicated input, this is so we can copy one audio, but process the other
ffmpeg \
-i 21.m4v \
-i 21.m4v \
-map 0:0 \
-c:v copy \
-map 0:1 \
-c:a:0 copy \
-map 1:1 \
-c:a:1 libfdk_aac
21_fixed.m4v

# Convert flash demo video to h264/m4v mono
ffmpeg \
-i dd6796a8c742c83457e1d7a32985cc5e.flv \
-c:v h264 \
-c:a libfdk_aac \
-ac:1 1 \
-ab:1 48k
SCID-workshop2.m4v

Video in 1 stream

ffmpeg -i video.mkv -i video.mkv \
  -map 0:1 -c:v:0 copy \                  # Copy video from stream 1
  -map 0:0 -c:a:0 copy \                  # Copy audio from stream 0
  -map 1:0 -c:a:1 libfdk_aac -ac:2 2 \    # Convert audio in stream 0 to AAC
  video.m4v

Helper Functions

# This is a ZSH function that you can use as a shortcut when your input file looks like:
#    Input #0, matroska,webm, from 'The.Americans.2013.S01E01.720p.WEB-DL-LFF.mkv':
#      Metadata:
#        creation_time   : 2013-02-15 09:11:09
#      Duration: 01:09:02.18, start: 0.000000, bitrate: 4365 kb/s
#        Stream #0:0(eng): Video: h264 (High), yuv420p, 1280x718 [SAR 1:1 DAR 640:359], 23.98 fps, 23.98 tbr, 1k tbn, 2k tbc #    (default)
#        Stream #0:1(eng): Audio: ac3, 48000 Hz, 5.1(side), s16, 384 kb/s (default)
#        Stream #0:2: Subtitle: subrip (default)

## Video-AudioAC3
function ff264_vac3() {
  ffmpeg \
  -i $1 \
  -i $1 \
  -map 0:0 \
  -c:v:0 copy \
  -map 0:1 \
  -c:a:0 copy \
  -map 1:1 \
  -c:a:1 libfdk_aac \
  -ac:2 2 ${1:r}.m4v
}
## AudioAC3-Video
function ff264_ac3v() {
  ffmpeg \
  -i $1 \
  -i $1 \
  -map 0:1 \
  -c:v:0 copy \
  -map 0:0 \
  -c:a:0 copy \
  -map 1:0 \
  -c:a:1 libfdk_aac \
  -ac:2 2 ${1:r}.m4v
}

## Full Conversion on all channels, if WMV or DIVX or whatever, assumes audio is on second(1) stream
function ff264_full() {
  ffmpeg \
  -i $1 \
  -i $1 \
  -map 0:1 \
  -c:v:0 h264 \
  -map 0:0 \
  -c:a:0 ac3 \
  -map 1:1 \
  -c:a:1 libfdk_aac \
  -ac:2 2
  ${1:r}.m4v
}

## Full Audio Conversion, such as when the MKV
#  is a BluRay rip and only has DTS
function ff264_audio_full() {
  ffmpeg \
  -i $1 \
  -i $1 \
  -map 0:0 \
  -c:v:0 copy \
  -map 0:1 \
  -ac:1 2 \
  -c:a:0 libfdk_aac \
  -map 1:1 \
  -c:a:1 ac3
  ${1:r}.m4v
}
## Convert & Move functions
function cvmv_ff264_vac3() { ff264_vac3 $1 && mvmedia ${1:r}.m4v }
function cvmv_ff264_ac3v() { ff264_ac3v $1 && mvmedia ${1:r}.m4v }

# Batch convert & move files
for f in *.mkv; do ff264_vac3 $f && mvmedia ${f:r}.m4v;  done

DTS/BluRay

$ ffmpeg -i LongKissGoodnight.mkv
Stream #0:0(eng): Video: h264 (High), yuv420p, 1280x536, SAR 1:1 DAR 160:67, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Metadata:
      title           : The Long Kiss Goodnight (1996)
    Stream #0:1(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), fltp, 1536 kb/s (default)
    Metadata:
      title           : DTS 5.1 1.5 Mbps
    Stream #0:2(eng): Subtitle: subrip

# AppleTV doesn't support DTS (does it?) so we output AAC (2ch) and AC3 (5.1)
$ ffmpeg -i LongKissGoodnight.mkv -i LongKissGoodnight.mkv \
  -map 0:0 -c:v:0 copy \
  -map 0:1 -ac:1 2 -c:a:0 libfdk_aac \
  -map 1:1 -c:a:1 ac3 LongKissGoodnight.m4v

Concatenate Files

for f in *.wav; do echo "file '$f'" >> mylist.txt; done
ffmpeg -f concat -i mylist.txt -c copy output.wav

Install

brew install ffmpeg \
--with-fdk-aac \
--with-ffplay \
--with-freetype \
--with-frei0r \
--with-libass \
--with-libcaca \
--with-libvo-aacenc \
--with-libvorbis \
--with-libvpx \
--with-opencore-amr \
--with-openjpeg \
--with-openssl \
--with-opus \
--with-rtmpdump \
--with-schroedinger \
--with-speex \
--with-theora \
--with-tools
Older article
MySQL Field Notes