Skip to content

Instantly share code, notes, and snippets.

@windyinsc
Forked from larvata/ffmpeg-cheatsheet.md
Last active September 7, 2023 12:15
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save windyinsc/d0aa4a8606283b7bdfd884f6984539cd to your computer and use it in GitHub Desktop.
Save windyinsc/d0aa4a8606283b7bdfd884f6984539cd to your computer and use it in GitHub Desktop.
ffmpeg notes

ffmpeg Cheatsheet

  • Join TS Files
  • Convert TS to MP4
  • Download Online AES-128 Encrypted HLS video
  • Convert Video to GIF
  • Extract Audio and Convert it to MP3
  • Burn Subtitles into Video

Join TS Files

ref: http://superuser.com/questions/692990/use-ffmpeg-copy-codec-to-combine-ts-files-into-a-single-mp4

# create ts filelist as following format:
file '<filepath>'
# or use following command to generate:
(for %i in (*.ts) do @echo file '%i') > ts.lst

# concat the ts files by ffmpeg
ffmpeg -f concat -i ts.lst -c copy all.ts

Convert TS to MP4

ref: https://ffmpeg.org/ffmpeg-bitstream-filters.html

ffmpeg -i <source.ts> -c copy -bsf:a aac_adtstoasc <target.mp4>

Download Online AES-128 Encrypted HLS Video

ref: http://forum.videohelp.com/threads/343902-Looking-for-ts-to-mp4-conversion-tool

# step 1: download m3u8 and key file
# step 2: modify key file path in m3u8 file
# step 3: download m3u8
ffmpeg [-protocol_whitelist "file,http,https,tcp,tls"] -i <source.m3u8> -c copy <target.ts>

Or just use:

ffmpeg -protocol_whitelist "file,http,tcp,https,tls,crypto" -i <source.m3u8> -c copy <target.ts>

Convert Video to GIF

ref: http://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality

# extract frames from video
mkdir frames
ffmpeg -i <input.mp4> -vf scale=320:-1 -r 10 frames/ffout%03d.png

# merge frames to gif
convert -delay 5 -loop 0 [-crop '100%x56%'] frames/ffout*.png <output.gif

Extract Audio and Convert it to MP3

ffmpeg -i <input.mp4> <out.mp3>

Burn Subtitles into Video

ref: https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo

ffmpeg -i video.avi -vf "yadif, ass=subtitle.ass" out.avi

Source via GitHubGist user Akagi201

video subtitle

ffmpeg -i *.mp4 -vf subtitles=*.srt output.mp4

element stream to ts

  • ffmpeg -i audio.aac -i ch2.h264 -acodec copy -vcodec copy -f mpegts out.ts

rtsp 2 rtmp

  • ffmpeg -rtsp_transport tcp -i rtsp://fuck.com/fuck/fuck -c copy -f flv rtmp://shit.com/shit/shit

截图片

  • ./ffmpeg -y -err_detect ignore_err -i "rtmp://uplive.b0.upaiyun.com/live/loading timeout=10" -f image2 -vframes 1 a.jpg
  • ./ffmpeg -y -err_detect ignore_err -i "rtmp://uplive.b0.upaiyun.com/live/loading live=1 timeout=10" -f image2 -vframes 1 a.jpg

推流图片

  • ./ffmpeg -loop 1 -i akhead.jpg -r 10 -f flv rtmp://uplive.v0.upaiyun.com/live/english

查看 ffmpeg 全部帮助

  • ffmpeg --help full

推纯音频

  • ffmpeg -re -i xxx.aac -vn -acodec copy -f flv -y rtmp://xxxx

实时转低码率

  • ffmpeg -i rtmp://10.0.5.135/uplive.b0.upaiyun.com/live/123 -b 1000k -s 960:540 -f flv rtmp://10.0.5.132/uplive.v0.upaiyun.com/live/small

循环推流

for((;;)); do \
    ./objs/ffmpeg/bin/ffmpeg -re -i ./doc/hightest_h1080p.mov \
    -vcodec copy -acodec copy \
    -f flv -y rtmp://127.0.0.1:1935/live/livestream; \
    sleep 1; \
done

纯 AAC 推流

  • ffmpeg -re -i time.aac -bsf:a aac_adtstoasc -c copy -f flv rtmp://uplive.b0.upaiyun.com/live/english

转码为低码率

  • ffmpeg -i time.flv -c libx264 -profile:v high -preset:v slow -b:v 300k -s 640x480 -r 25 -acodec libfdk_aac -ac 2 -b:a 48k -ar 44100 -y time.300kbps.flv

切割mp4

  • ffmpeg -ss 00:00:00 -i mpeg4conformance.mp4 -vcodec copy -acodec copy -t 00:00:10 o.mp4

解析所有的帧

webcam

常用命令

下载m3u8视频

  • ffmpeg -i M3U8 URL -c copy mpegts.ts

桌面直播

  • ffmpeg -f x11grab -follow_mouse centered -r 100 -s cif -i :0.0 -f flv $RTMP_PUSH_URL

转h.264裸流文件

  • -acodec none

watermark

使用FFMPEG发布RTSP流

  • 上传: ffmpeg -i a.mp4 -vcodec libx264 -f rtsp rtsp://127.0.0.1:5050/ok
  • 接收: ffmpeg -rtsp_flags listen -f rtsp -i rtsp://127.0.0.1:5050/ok b.mp4

压制

转码到webm

webcam capture

Common ffmpeg Commands

via GitHubGist user eyecatchup

cut sequence from mp4 video

ffmpeg -ss <start_time_hh:ii:ss> -i input.mp4 -to <length_in_hh:ii:ss_from_start> -c copy ./out.mp4
ffmpeg -ss 00:47:42 -i input.mp4 -to 00:01:49 -c copy ./out.mp4

create thumbnail images from input video (@30fps)

ffmpeg -i input.mp4 -r 30/1 ./out-%03d.jpg

concatinate multiple video files

ffmpeg -f concat -i filelist.txt -c:v copy ./out.mp4

Contents if filelist.txt:

file input1.mp4
file input2.mp4

Note: Must call ffmpeg in path of filelist.txt and use relative paths in filelist.txt!


add blur to video

Blur video:

ffmpeg -i input.mp4 -vf "boxblur=5:1" ./out.mp4

Add blur overlay to specific area:

ffmpeg -i input.mp4 -filter_complex "[0:v]crop=<width_px>:<height_px>:<margin_left_px>:<margin_top_px>,boxblur=<chroma_level_1-9>[fg]; [0:v][fg]overlay=<margin_left_px>:<margin_top_px>[v]" -map "[v]" -map 0:a -c:v libx264 -c:a copy -movflags +faststart ./out.mp4
ffmpeg -i input.mp4 -filter_complex "[0:v]crop=120:40:60:40,boxblur=9[fg]; [0:v][fg]overlay=60:40[v]" -map "[v]" -map 0:a -c:v libx264 -c:a copy -movflags +faststart ./out.mp4

More examples (e.g. how to use masks)


save RMTP video stream to local flv video

ffmpeg -i rtmp://web1.iptv-planet.com:81/live2/ -y -f flv ./out.flv

save M3U8 HTTP video stream to local mp4 video

ffmpeg -i "http://example.com/playlist.m3u8" -c:v copy -bsf:a aac_adtstoasc ./out.mp4

Reference for "aac_adtstoasc"


convert avi video to mp4 video

If you have libfaac:

# mpeg4 (native) -> mpeg4 (native)
ffmpeg -i input.avi -c:v libx264 -crf 19 -preset slow -c:a libfaac -b:a 192k -ac 2 out.mp4
ffmpeg -i input.avi -c:v libx264 -crf 19 -preset slow -c:a aac -strict experimental -b:a 192k -ac 2 out.mp4

If you don't have libfaac:

# mpeg4 (native) -> mpeg4 (native)
# mp3 (native) -> aac (native)
ffmpeg -i input.avi -acodec libfaac -b:a 192k -vcodec mpeg4 -b:v 1200k -flags +aic+mv4 ./out.mp4
ffmpeg -i input.avi -acodec aac -strict experimental -b:a 192k -vcodec mpeg4 -b:v 1200k -flags +aic+mv4 ./out.mp4
# mpeg4 (native) -> h264 (libx264)
ffmpeg -i input.avi -c:v libx264 -crf 22 -preset medium -c:a aac -strict experimental -b:a 192k -ac 2 ./out.mp4
# mpeg4 (native) -> h264 (libx264)
# mp3 (native) -> aac (native)
ffmpeg -i input.avi -c:v libx264 -crf 23 -profile:v high -r 30 -c:a aac -strict experimental -q:a 100 -ar 48000 out.mp4
# alternatives: -b:a 128k
#               -b:a 192k
#               -crf 19 -preset slow
#               -crf 22 -preset medium

convert flv video to mp4 video

ffmpeg -i input.flv -vcodec copy -acodec copy ./out.mp4

ffmpeg -i input.flv -c:v copy -bsf:a aac_adtstoasc ./out.mp4

Reference for "aac_adtstoasc"

If the error Could not find tag for codec vp6f in stream #0, codec not currently supported in container occurs, try:

ffmpeg -i input.flv -c:v libx264 -crf 23 -preset medium -c:a aac -strict -2 -b:a 128k -ar 44100 -ac 2 ./out.mp4

convert wmv video to mp4 video

ffmpeg -i file.wmv -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -preset medium -crf 22 -movflags +faststart ./out.mp4

ffmpeg -i input.wmv -c:v libx264 -crf 23 -preset medium ./out.mp4

ffmpeg -i input.wmv -c:v libx264 -crf 23 -c:a libfaac -q:a 100 ./out.mp4

ffmpeg -i input.wmv -c:v libx264 -crf 23 -profile:v high -r 30 -c:a libfaac -q:a 100 -ar 48000 ./out.mp4

ffmpeg -i input.wmv -c:v libx264 -crf 23 -profile:v high -r 30 -c:a aac -strict experimental -q:a 100 -ar 48000 ./out.mp4

Legend + References

Special FAAC switches & bitstream filters

FAAC is an MPEG-4 and MPEG-2 AAC encoder.

  • -flag
    The flag command is used to by calling "-flag", followed by a single space and then all flags with a plus "+" or minus "-" sign preceding each indicating active or inactive flag respectively.
  • +aic
    h263 advanced intra coding / mpeg4 ac prediction (Affects: Encoding, Video)
  • +mv4
    Use four motion vector by macroblock (mpeg4) (Affects: Encoding, Video)
  • -bsf:a aac_adtstoasc
    Convert MPEG-2/4 AAC ADTS to an MPEG-4 Audio Specific Configuration bitstream. Is required for example when copying an AAC stream from a raw ADTS AAC or an MPEG-TS container to MP4A-LATM, to an FLV file, or to MOV/MP4 files and related formats such as 3GP or M4A.

Install FFmpeg with MP3 support on macOS

Instructions for installing the command-line media conversion program FFmpeg with MP3 support on macOS. via GitHubGist user stevemclaugh

Initial Setup

If Homebrew is already installed, skip to "Install FFmpeg" below.

Launch Terminal in macOS, located at /Applications/Utilities/Terminal.app. To install a bundle of command-line tools provided by Apple, paste the following line into the terminal window and press return. When prompted, click "Install," then "Agree."

xcode-select --install

Run the following command to install the Homebrew package manager. Enter your password at the prompt to proceed.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Next, enter this command to update Homebrew.

brew update

If Homebrew returns an error, enter the following command to give it the permissions it expects.

sudo chown $(whoami):admin /usr/local && sudo chown -R $(whoami):admin /usr/local

Install FFmpeg

First, run the following command to remove FFmpeg in case it's already installed.

brew uninstall --force --ignore-dependencies ffmpeg

Now let's install the required compilation utilities and media codecs.

brew install automake fdk-aac git lame libass libtool libvorbis libvpx opus sdl shtool texi2html theora wget x264 xvid yasm

Next we'll download FFmpeg's source code and compile it before installing. Although it's faster to install FFmpeg through Homebrew, that version can't read or write MP3 files.

Enter the following commands one at a time; note that the third line is very long. After the last command you will be prompted to enter your admin password.

git clone http://source.ffmpeg.org/git/ffmpeg.git ffmpeg

cd ffmpeg

./configure  --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid

make && sudo make install

The final step above will take 5 or 10 minutes to complete. When it's done, open a new terminal window and enter the following to view FFmpeg's manual.

man ffmpeg

If you've made it this far, you should be all set. Press q to close the manual.

Here's a basic FFmpeg command to convert a file called input.wav on your desktop to a 320Kbps MP3 called output.mp3.

cd ~/Desktop

ffmpeg -i input.wav -ab 320k output.mp3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment