Log on
Main page Graphics Photography Music & Audio Audio Plugins Video Tools Web Design Documents Space Astro Amiga Funny Surreal Gallery Links & Contact

Video related unix commands


This is a collection of various little command lines that I figured out, and that I just want to put somewhere instead of having to figure them out again, the next time I need them. I cannot guarantee they will still work in the future, as some of the tools involved may change their syntax or behavior. But they did work when I put them here.

Creating an animated gif from frames

magick -delay 20 -loop 0 frame*.png output.gif
Or straight from a camera with a bit of sharpen:
magick *.jpg -delay 15 -loop 0 -resize 250x250 -sharpen 0.5 -auto-orient output.gif
Or from frames grabbed from an anime:
magick -delay 10 -loop 0 *.png -resize 500x500 -sharpen 0.3 -dither FloydSteinberg -define dither:diffusion-amount=80% -layers OptimizeTransparency +remap output.gif

Extracting the AAC audio from an .mp4 file

ffmpeg -i input.mp4 -vn -acodec copy -y output.m4a

Remuxing several .ogm files into .mkv

find *.ogm -exec mkvmerge -o "{}.mkv" "{}" \;

Multiplexing MP4 losslessly

This command will discard any audio from the input video file.
MP4Box -add "video.mp4"#video -add "audio.m4a"#audio -new bla7.mp4

Finding out what codecs are used in all your MKV files

find . -iname "*.mkv" -exec mkvinfo "{}" \; |grep -i "codec id"

Nikon D7000/D600 quicktime video to mp4 (aac audio)

Ffmpeg's syntax changes, so here are two approaches. One of them should work:
ffmpeg -i _DSC0544.MOV -vcodec copy -acodec libfaac -ab 160000 test.mp4 ffmpeg -i _DSC0544.MOV -vcodec copy -acodec aac -ab 160000 -strict -2 test.mp4

JPEG sequence to H.264 (high-def timelapse, 10 fps, no audio)

Tip: Replace the % character with %% if using this in a .bat file in Windows.
ffmpeg -r 10 -i "frame%04d.jpg" -y -an -vcodec libx264 -crf 12 -preset veryslow -s 1920x1080 output.mp4

PNG sequence and .wav to H.264 (no scaling, 30 fps, with audio)

Replace "veryslow" with "fast" if you're impatient.
ffmpeg -r 30 -i "frame%04d.png" -i sound.wav -y -vcodec libx264 -crf 12 -pix_fmt yuv420p -preset veryslow -acodec libfaac -ab 160000 output.mp4

Slideshow from jpeg images

This will show a new image every 3 seconds and produce a video at 10 fps. Make sure your input images are the same size, in this example: 1920x1080. The part that says -pix_fmt yuv420p is to make it work in Firefox and Chrome.
ffmpeg -loop 1 -framerate 1/3 -i pic%04d.jpg -y -i music.mp3 -acodec copy -vcodec libx264 -crf 17 -preset slow -s 1920x1080 -shortest -r 10 -pix_fmt yuv420p output.mp4

Losslessly keep section of .mkv file

The -ss part shows where to start, and the -t part shows the duration. Ffmpeg does not seem to have a way to start at a keyframe, so you will have to try changing the start and end point until the result behaves correctly.
ffmpeg -i input.mkv -acodec copy -vcodec copy -ss 00:00:28 -t 00:01:35 output.mkv

Audio track from video file to .wav

ffmpeg -i input.mp4 -vn audio_only.wav

Converting MKV (h264/aac) to MP4 in FreeBSD (1st method)

mkvextract tracks source.mkv 1:video.264 2:audio.aac 3:subtitles.ass mp4box -add video.264 -add audio.aac new.mp4

Converting MKV (h264/ac3) to MP4 in FreeBSD (2nd method)

#/bin/sh # usage: makemp4 <mkv filename> <framerate, e.g. 23.976> BASENAME=`echo $1|sed s/.mkv$//` mkvextract tracks "$1" 1:video.264 2:audio.ac3 3:"$BASENAME.ass" 4:"$BASENAME-b.ass" mplayer audio.ac3 -af channels=2 -ao pcm:fast:waveheader:file=audio.wav -channels 2 -novideo 2>&1 normalize --clipping --peak audio.wav faac -q 133 audio.wav audio.aac rm "$BASENAME.mp4" mp4box "$BASENAME.mp4" -fps $2 -add video.264 rm video.264 audio.wav audio.ac3 mp4box "$BASENAME.mp4" -add audio.aac rm audio.aac

Splitting a video into 5 second pieces

ffmpeg -i "input.mkv" -c copy -map 0 -segment_time 5 -f segment output%03d.mkv

And putting them back together:
printf "file '%s'\n" ./*.mkv > mylist.txt ffmpeg -f concat -i mylist.txt -sn -acodec copy -vcodec copy output.mkv

Create XviD .avi file for audio/video sync

This will generate an intermedia .avi file suitable for fast seeking in a DAW like Samplitude - something sound designers typically use when adding sound effects to a piece of video. Don't use these settings for the final video encode, as it does not look very pretty.
ffmpeg -i input.mp4 -an -vcodec libxvid -g 4 output.avi

Encode video but use audio from .wav file instead of source video

ffmpeg -i input.mp4 -i input.wav -c:v copy -map 0:v:0 -map 1:a:0 output.mp4

Avi to Discord compatible H.264

ffmpeg -i input.avi -vcodec libx264 -crf 20 -preset veryslow -pix_fmt yuv420p -acodec aac -ab 160000 output.mp4
Small video with white balance, filmed with Pixel 4a:
ffmpeg -y -i input.mp4 -vf scale=640:-1,eq=gamma_r=0.8,eq=gamma_g=0.8,eq=gamma_b=0.8,eq=saturation=0.9 -vcodec libx264 -crf 25 -preset veryslow -pix_fmt yuv420p -acodec aac -ab 64000 output.mp4

Ultra hard compression with AV1 and Opus

ffmpeg -i input.avi -vcodec libsvtav1 -crf 40 -preset 4 -pix_fmt yuv420p10le -acodec libopus -ab 48000 output.mp4

Make file readable by Davinci Resolve

ffmpeg -y -i "input.mp4" -vcodec prores_ks -vsync passthrough -acodec copy output.mov

Video - Experimental


Reverse a video

Method one uses a lot of disk space but works better:
ffmpeg -i "input.mkv" -vf reverse -af areverse -c:v rawvideo -pix_fmt yuv420p -c:a pcm_s16le -y "temp.mkv" ffmpeg -i "temp.mkv" -vcodec libx264 -crf 20 -pix_fmt yuv420p -preset veryslow -acodec aac -ab 160000 -y "output.mp4" rm "temp.mkv"
Method two will require less disk space, but a very large amount of memory and may output corrupt files because ffmpeg doesn't quite like doing this:
ffmpeg -i input.mp4 -vf reverse -af areverse -c:v libx264 -intra -c:v libx264 -preset slow -crf 25 -b:a 160k -c:a aac output.mp4

Average or median a video

This one works in linux/bsd but not in Windows.
ffmpeg -i input.mp4 -r 1/1 frame%04d.bmp convert -background black *.bmp -evaluate-sequence median median.png convert -background black *.bmp -evaluate-sequence mean mean.png

Image


Convert all .webp images into jpeg

magick mogrify -format jpg -quality 90 *.webp
Even better quality, yet retaining color for even completely transparent areas:
magick mogrify -format jpg -quality 90 -define webp:method=6 -define webp:use-sharp-yuv=1 -define webp:exact=true -define webp:alpha-compression=1 -define webp:alpha-filtering=2 *.webp

Powerful .png compression

Strategy 0 (default) and 1 (filtered) appear to give the smallest files. The default settings appear to be one of these, so the easiest is probably to leave them out and only specify compression level 9.
magick input.png -define png:compression-level=9 -define png:compression-filter=0 -define png:compression-strategy=0 output.png

Tile .jpg and .webp images

magick montage -format png -background black -geometry "700x700^" +repage -gravity center -crop "700x700+0+0!" -tile 4x3 -sharpen 1,2 *.jpg *.webp new.png

Tile .png files into .jpg for UI image strip

magick montage -border 0 -tile 1 -geometry 100% *.png -sharpen 1x0.5 -quality 88 tiled.jpg

Tile .png files into .jpg for UI image strip, 512px wide

magick montage -border 0 -tile 1 -geometry 512 *.png -sharpen 1x0.5 -quality 88 tiled.jpg

Resize many .jpg images to 512px .png

Useful for preparing a bunch of images for Stable Diffusion.
magick mogrify -resize "512x512^" -gravity Center -crop 512x512+0+0 -sharpen 0.7 -format png *.jpg

Example dcraw conversion

dcraw -T -w -g 10 10 -q 3 -m 10 "_dsc1234.dng"

Sound


Unify any input format to 32khz 16bit mono .wav 1 sec long

sox snd04.wav -c 1 -b 16 new04.wav norm rate 32000 fade 0 1.05 0.1 trim 0 1

Split a .wav file into separate .wav files on transients. This is useful when having recorded a lot of single sounds like e.g. hitting drums one by one in order to create a sample collection.
sox WoodRecordings_titan_T001.wav "2pct/bla.wav" silence 1 0.1 2% 1 0.1 1% : newfile : restart

Trim silence from beginning of multiple stereo .wav files and then split into dual mono. (Useful when batch editing sound samples.)
mkdir "../output_left" && mkdir "../output_right" find input*.wav -exec sox "{}" "../output_left/{}" silence -l 1 0.001 0.1% 0 0.1 0.1% remix 1 \; find input*.wav -exec sox "{}" "../output_right/{}" silence -l 1 0.001 0.1% 0 0.1 0.1% remix 2 \;

Convert wav into mp3

ffmpeg -i input.wav -codec:a libmp3lame -compression_level:a 1 -b:a 192000 output.mp3

Surround Sound


Surround multichannel .wav into AAC

Channel order should be FL FR Center LFE SL SR, which corresponds to Samplitude's default export.
ffmpeg -i input.wav -ac 6 -c:a aac -b:a 448k output.m4a

Surround aac to .wav

ffmpeg -i input.mp4 -filter_complex "channelsplit=channel_layout=5.1[FL][FR][FC][LFE][BL][BR]" -map "[FL]" front_left.wav -map "[FR]" front_right.wav -map "[FC]" front_center.wav -map "[LFE]" lfe.wav -map "[BL]" back_left.wav -map "[BR]" back_right.wav

Surround multichannel .wav into ac3

Assuming channel order L,R,center,lfe,Ls,Rs.
ffmpeg -i multichannelwav.wav -vn -acodec ac3 -ac 6 -ar 48000 -ab 448k -dialnorm -24 -dsur_mode 0 -original 1 -dmix_mode 2 -channel_layout 63 result.ac3

Turn DTS surround audio into mp3

ffmpeg -i "input.mkv" -vcodec copy -scodec copy -acodec libmp3lame -ac 2 -ab 192000 "output.mkv"

Turn DTS surround audio into mp4

This approach assumes H.264 video and no subtitles.
ffmpeg -i "input.mkv" -vcodec copy -scodec copy -acodec libfaac -ac 2 -ab 160000 "output.mp4"


Website by Joachim Michaelis