Streaming video in real time (which is cool, cause you can pass this to f in matcherservice and indexer-service

ffmpeg -re -i ~/Desktop/kung-fury-channel/kung_fury_ads.mp4 -acodec copy -vcodec copy -f mpegts udp://127.0.0.1:9000
ffmpeg -re -i ~/projects/videos/kung-fury-channel/kung_fury_ads.mp4 -q 0 -f mpegts udp://localhost:9000





Scaling, width: 1/2,  height scaled accordingly to preserve aspect ratio

ffmpeg -i input -vf scale=iw/2:-1 output

Add black pads on margin

ffmpeg -i input_file -vf "pad=output_width:output_height:sizeofwidth:siezeofheight:color" output_file.mp4

Convert to 1080p

ffmpeg -i KidsPlay4x3.mp4 -vf scale=1920:1080 KidsPlay_1080p.mp4
ffmpeg -i MLFLong.mxf -vf "movie=squeeze_icon.png [watermark]; [in][watermark] overlay=main_w-overlay_w-20:main_h-overlay_h-20 [out]" MLFLongLogo.mp4

Increase contrast by 25%(https://ffmpeg.org/ffmpeg-filters.html#eq)

ffmpeg -i MLFLong.mxf -vf eq=1.25:0:1:1:1:1:1:1 MLFLongContrast125Percent.mp4

Change video bit rate to 200K

ffmpeg -i zombie.mp4 -b:v 200k zombie_v200k.mp4

Change frame rate to 10 frames per second

ffmpeg -i zombie.mp4 -r 10 zombie_fps10.mp4

Cut 19 seconds of video and audio from cbs_1b_watch.mxf

ffmpeg -i cbs_1b_watch.mxf -ss 00 -c copy -t 19 geico.mxf

Figure out # of frames in video

ffmpeg -i ~/Desktop/TimeCode_1080p.mov -f null /dev/null

General information about a file

ffmpeg -i file

Draw Box(https://ffmpeg.org/ffmpeg-filters.html#drawbox)

ffmpeg -i input_file drawbox=x=10:y=20:w=200:h=60:color=red output_file

Convert HLS to MP4

ffmpeg -i playlist.m3u8 -bsf:a aac_adtstoasc -vcodec copy video.mp4


Splice 

ffmpeg -i input.wmv -ss 00:00:30.0 -c copy -t 00:00:10.0 output.wmv
ffmpeg -i input.wmv -ss 30 -c copy -t 10 output.wmv

Get Every Frame of a Video

// read frames from movie saving to jpeg

ffmpeg -i file.mpg -r 1/1 $filename%03d.jpg


Get Only One Frame

// extract one frame, starting at second 20, get one frame

ffmpeg -i 1.1Burger_King.mp4 -ss 00:00:20.000 -vframes 1 out.png

Crop out 10%

ffmpeg -i MLFLongScaleUp10Percent.mp4 -vf crop=1920:1080:96:54 MLFLongScaleCrop10Percent.mp4
or 
// origin at (0.05w, 0.05h) with size 0.9w x 0.9h
ffmpeg -i 1.1Burger_King.mp4 -vf crop=0.9*in_w:0.9*in_h:0.05*in_w:0.05*in_h 1.1Burger_King_crop_out_10percent.mp4
 
 
//On Mac
ffmpeg -i 1.1Burger_King.mp4 -vf "crop=0.9*in_w:0.9*in_h:0.05*in_w:0.05*in_h" 1.1Burger_King_crop_out_10percent.mp4

Pixelation

ffmpeg -i input -qscale # output

Where # = 0 preserves the quality of the input video, and the higher the number the more pixilation (noticeable at >15-20) 

Closed Captions / Subtitles

(text only)

ffmpeg -i input -vf subtitles=subfile output

(text with black box border outline)

ffmpeg -i input -vf subtitles=subfile:force_style='BorderStyle=3' output

Where subfile = .srt format subtitle file.

Rotate Video

ffmpeg -i input -vf transpose=# output

Where # = 0,1,2,3 for Vertical Flip, 90 degrees Clockwise, 90 degrees Counterclockwise, 90 degrees Clockwise + Vertical Flip.

Adjust Volume

(1/10x)

ffmpeg -i input -af "volume=0.1" output

(10x)

ffmpeg -i input -af "volume=10" output

Picture in Picture

(Top Right Corner)

ffmpeg -i input -r 29.97 -vf "movie=small, scale=212:120 [vid2]; [in][vid2] overlay=main_w-overlay_w-10:10" output

Where small = the smaller embedded video.

The following PIP is a working example using chain of filters.

// Scale ad.mp4 by half, and overlay it on top of main.mp4, overlaid at middle of main.mp4

ffmpeg -i main.mp4 -i ad.mp4 -filter_complex "[1]scale=iw/2:ih/2 [pip]; [0][pip] overlay=main_w/2:main_h/2"  pip_output.mp4

Adjust Brightness

(darker, val * (< 1))

ffmpeg -i input -vf "lutyuv=val*0.5" output

(brighter, val * (> 1))

ffmpeg -i input -vf "lutyuv=val*2" output


Splicing Video

ffmpeg -i input.wmv -ss 00:00:30.0 -c copy -t 00:00:10.0 output.wmv
ffmpeg -i input.wmv -ss 30 -c copy -t 10 output.wmv


Simulate sending frames (and then playing them back) over a TCP port

Simulate sending frames
ffmpeg -i video.mp4 -f avi -vcodec rawvideo -pix_fmt yuv420p -acodec pcm_alaw tcp://localhost:1234
ffplay tcp://localhost:1234?listen

Correct Timestamps

Correct Timestamps
ffmpeg -i video.mp4 -vsync drop -f avi -vcodec rawvideo -pix_fmt yuv420p -acodec pcm_alaw tcp://localhost:1234


Get Number of Frames From Video File

ffprobe -select_streams v -show_streams constant-frame-rates/1_Jar_jar_Gecko_Commercial.mp4 | grep nb_frames

Install imagemagick

required for 'convert' and 'display' commands used below.

Install imagemagick
brew install imagemagick # on Mac
sudo apt-get install imagemagick # on Ubuntu

Convert between Raw Y bytes files and other common image formats


Convert y <-> png
convert -size 852x480 -depth 8 gray:TimeCode57.y TimeCode57.png # y -> png
convert TimeCode57.png -depth 8 gray:TimeCode57.y # png -> y


Display raw y frame

display -size 256x256 -depth 8 gray:image.y

Or you can always set the output file on a convert command to "x:" which will display instead of save:

convert -size 852x480 -depth 8 gray:TimeCode57.y x:


Add Frame number or timecode to any video

Requires ffmpeg --with-freetype

ffmpeg -i test2.mov  -vf "drawtext=fontfile=Arial.ttf: text=%{n}: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099" -y output.mov

On Linux you need to specify the whole font path: /usr/share/fonts/truetype/msttcorefonts/Arial.ttf


ffmpeg -i TimeCode.mov -r 30 -vf "[in]setpts=0.7992*PTS[middle1];[middle1]scale=960:540[middle] ; [middle]drawtext=fontfile=/Library/Fonts/Arial\ Narrow\ Bold.ttf: text='':timecode='00\:00\:00\:00':r=30: fontcolor=white: fontsize=35: x=750: y=430[out]" -filter:a "atempo=1.25125125125" TimeCode540p.mp4

This one takes every .mp4 file in a directory and rescales it to 1920x1080, and adds a frame counter on the top and bottom left. (Two counters to detect possible tearing artefacts while rendering from ffplay)

for i in `ls *.mp4` ; do ffmpeg -i ${i} -vf "[in]scale=1920:1080[middle];[middle]drawtext=fontfile=/Library/Fonts/Arial.ttf: text=%{n}: x=lh/2: y=h-(3*lh/2): fontcolor=white: box=1: boxborderw=4: boxcolor=0x000000ff: fontsize=32[middle2];[middle2]drawtext=fontfile=/Library/Fonts/Arial.ttf: text=%{n}: x=lh/2: y=lh/2: fontcolor=white: box=1: boxborderw=4: boxcolor=0x000000ff: fontsize=32[out]" -y 1080p/${i} ; done 



Rename captures frames without timecode

Using rename
sudo apt-get install perl # On Ubuntu - rename comes with perl
brew install rename # On Mac
rename 's/(frame.*)-[^\.]*(.*)/$1$2/g'*
Using pure bash
for file in frame*-*.y; do mv $file ${file%%-*.y}.y; done


This renames all files with names like frame213-143920102.y to frame213.y.


Linux (Ubuntu) installation instructions

The instructions on this page will allow you to compile ffmpeg from source on Ubuntu, Debian, or Mint, including many important libraries.

https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu


Reverse video frames (Rotate 180 degrees)

ffmpeg -i input.mp4 -vf "transpose=2,transpose=2,format=yuv420p" -metadata:s:v rotate=0 -codec:v libx264 -codec:a copy output.mp4

(from http://superuser.com/a/578329/152361)

+ Recent posts