└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Video and Image Manipulation on the Command Line 2 | 3 | ## ffmpeg 4 | 5 | ### Getting Help 6 | 7 | `ffmpeg -h encoder=tiff` 8 | 9 | `ffmpeg -h encoder=mjpeg` 10 | 11 | `ffmpeg -pix_fmts > %filepath%\formats.txt see all the formats` 12 | 13 | `ffprobe -i input.mp4` 14 | ffprobe get's video metadata 15 | 16 | #### 4K H.265 17 | 18 | `ffmpeg -y -i file.%%04d.tif -c:v libx265 -x265-params bitrate=25000:vbv-bufsize=8000:vbv-maxrate=40000 -pix_fmt yuv420p -movflags +faststart -f mp4 name.mp4` 19 | 20 | 21 | #### 4k H.265 (less compression) 22 | 23 | `ffmpeg -y -i in.mov -c:v libx265 -x265-params bitrate=27000:vbv-bufsize=8000:vbv-maxrate=40000 -pix_fmt yuv420p -movflags +faststart -f mp4 name.mp4` 24 | 25 | 26 | #### H.264 mp4 27 | 28 | `ffmpeg -y -i in.mov -c:v libx264 -x264-params bitrate=25000:vbv-bufsize=8000:vbv-maxrate=30000 -pix_fmt yuv420p -movflags +faststart -f mp4 name.mp4` 29 | 30 | 31 | #### APPLE PRORES (Best for files playing on a Mac) 32 | 33 | `ffmpeg -y -i in.mov -vcodec prores -profile:v 3 -r "30" -c:a mp2 %filepath%\MKprores.mov` 34 | 35 | 36 | #### PHOTO JPG (Best for files playing on a PC) 37 | 38 | `ffmpeg -y -r 30 -start_number 1 -i %file_path%/%project%.%04d.tif -pix_fmt yuvj420p -vframes 10 -vcodec mjpeg -q:v 7 -f mov name.mov` 39 | 40 | Other options: 41 | 12 bit yuvj420p Adobe uses yuvj420p, files smaller 42 | 16 bit yuvj422p 43 | 24 bit yuvj444p double size 44 | 45 | 46 | #### HAP Q for playback on a quadcore machine 47 | 48 | `ffmpeg -y -i in.mov -c:v hap -format hap_q -chunks 4 -f mov out.mov` 49 | 50 | Install the codec from [the Vidox QT plugin repository](https://github.com/Vidvox/hap-qt-codec/releases/tag/version-12) for playback, and see [instructions for install with ffmpeg here](https://gist.github.com/dlublin/e4585b872dd136ae88b2aa51a6a89aac). 51 | 52 | Note that the number of 'chunks' can be a number 1-64 but should not exceed the number of CPU cores in your playback machine. 53 | 54 | Other format options: 55 | hap (smaller file size but less quality) 56 | hap_alpha (same as hap, but with support for alpha channel) 57 | hap_q_alpha (hap_q with alpha channel) 58 | 59 | 60 | #### TIFF to TIFF compress LZW and change to grayscale 61 | 62 | `ffmpeg -y -i file.%%04d.tif -compression_algo lzw -pix_fmt gray out.%%04d.tif` 63 | 64 | #### Scale 65 | 66 | `ffmpeg -i input.mp4 -vf scale=newW:newH out.mp4` 67 | 68 | #### Nearest Neighbor Scaling 69 | 70 | `ffmpeg -i in.mov -sws_flags neighbor -vf scale=newWidth:newHeight out.mov` 71 | 72 | #### Pipe IO 73 | 74 | How to send and receive data to/from named pipes (mac / linux only) 75 | 76 | Make two pipes 77 | 78 | `mkfifo /tmp/pipe1 /tmp/pipe2` 79 | 80 | Send input1 to pipe1 81 | 82 | `ffmpeg -y -i input1.mp4 -c copy -f nut /tmp/pipe1` 83 | 84 | In another shell window 85 | 86 | `ffmpeg -y -i input2.mp4 -c copy -f nut /tmp/pipe2` 87 | 88 | In another shell concat the videos 89 | 90 | `ffmpeg -y -i /tmp/pipe1 -i /tmp/pipe2 -filter_complex concat=n=2:v=1 out.mov` 91 | 92 | Might be possible to do this in one shell by appending a single ampersand to the end of each ffmpeg command. 93 | 94 | #### Add title bars 95 | 96 | `ffmpeg -i in.mp4 -vf pad=newWidth:newHeight:newX:newY out.mp4` 97 | 98 | Default pad color is black 99 | 100 | newX and newY are how far to move (0,0) from it's old origin. 101 | 102 | See: https://ffmpeg.org/ffmpeg-filters.html#Examples-65 103 | 104 | #### Make extra long looping video 105 | `for i in {1..60}; do printf "file '%s'\n" videoToLoop.mp4 >> list.txt; done` 106 | 107 | `ffmpeg -f concat -i list.txt -c copy out.mp4` 108 | 109 | See: https://trac.ffmpeg.org/wiki/Concatenate 110 | 111 | #### Mux audio and video (without re-encoding) 112 | 113 | `ffmpeg -i video.mp4 -i music.mp3 -c copy -map 0:v:0 -map -map 1:a:0 -shortest out.mp4` 114 | 115 | Muxing an mp4 with mp3 won't work in all players, but audio can be converted to wav or aac if you need more compatibility. 116 | 117 | #### Crop 118 | 119 | `ffmpeg -i input.mp4 -filter:v "crop=out_w:out_h:x:y out.mp4` 120 | 121 | X and Y denote top left of output. 122 | 123 | #### Trim 124 | 125 | `ffmpeg -i input.mp4 -ss 00:00:03 -t 00:01:03 -codec copy -async 1 out.mp4` 126 | 127 | SS is the new start time of the video 128 | 129 | T is the new length of the video 130 | 131 | Codec copy is faster than decoding/recoding 132 | 133 | async 1 is needed to ensure audio is correctly chopped 134 | 135 | See: http://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg 136 | See: https://trac.ffmpeg.org/wiki/Seeking 137 | 138 | #### Resize 139 | 140 | `ffmpeg -i input.mp4 -vf scale=newWidth:newHeight output.mp4` 141 | 142 | 143 | #### Invert Colors 144 | 145 | `ffmpeg -i input.mp4 -vf lutrgb="r=negval:g=negval:b=negval" inverted.mp4` 146 | 147 | #### Convert to Grayscale 148 | 149 | `ffmpeg -i input.mpt -vf "hue=s=0" -c:a copy grayscale.mp4` 150 | 151 | #### Rotate 152 | 153 | `ffmpeg -i input.mp4 -vf rotate=90 rotated90Degrees.mp4` 154 | 155 | Rotate is in degrees 156 | 157 | #### Extract frames from video 158 | 159 | `ffmpeg -i input.mp4 -vf fps=1 -f image2 savedFrames/%05d.jpg` 160 | 161 | fps = 1 will save 1 frame per second 162 | fps = 30 will save 30 frames per second 163 | fps = 1/60 will save 1 frame per minute 164 | 165 | See: https://trac.ffmpeg.org/wiki/Create%20a%20thumbnail%20image%20every%20X%20seconds%20of%20the%20video 166 | 167 | #### Compress audio 168 | 169 | `ffmpeg -i inputfile.mp3 -b:a 64k outputfile.mp3` 170 | 171 | #### Make video from image sequence 172 | 173 | First rename your images so that they are padded with 0 zeros eg. (img00000.jpg ... 174 | img00253.jpg). Finder can do this if you select all images, right click, rename 175 | images > format with name and counter starting from 0. 176 | 177 | cd into your directory of images. 178 | 179 | `ffmpeg -framerate 30 -i img%05d.jpg -c:v libx264 -r 30 -pix_fmt yuv420p 180 | ../video.mp4` 181 | 182 | #### Make videos from folder of folders of image sequences 183 | 184 | first cd into the folder with all your image sequences folders. 185 | make sure they are named in order from 00000.jpg to however many frames you have 186 | 187 | `for d in *; do ffmpeg -framerate 15 -i $d/%05d.jpg -c:v libx264 -r 15 -pix_fmt yuv420p $d.mp4; done` 188 | 189 | #### Convert gif to mp4 190 | 191 | `ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4` 192 | 193 | Taken From [here](http://unix.stackexchange.com/questions/40638/how-to-do-i-convert-an-animated-gif-to-an-mp4-or-mv4-on-the-command-line) 194 | 195 | Can follow up this command with the create extra long looping video from above if you want a longer loop. 196 | 197 | #### Change framerate 198 | `ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" out.mp4` 199 | 200 | 2.0 Would be half as slow, 0.5 would be twice as fast 201 | 202 | #### Batch Convert 203 | 204 | ```` 205 | for i in *.mp4 ; do 206 | ffmpeg -i "$i" -c:v libx264 -maxrate 10000k -bufsize 5000k $(basename "${i/.flac}").mp4 207 | done 208 | 209 | ```` 210 | 211 | 212 | ## imagemagick 213 | 214 | #### Getting help 215 | 216 | `convert -h` 217 | 218 | `identify -list format` 219 | Will show you supported formats on your machine 220 | 221 | #### Show image information 222 | 223 | `identify -verbose image.jpg` 224 | 225 | #### Convert file format 226 | `convert in.jpg out.tif` 227 | 228 | Can be any image file type in or out (unless RAW) 229 | 230 | #### Batch Resize 231 | 232 | `mogrify -path outputFolder -resize newWidthxnewHeight *.png` 233 | 234 | Output folder is an already existing destination 235 | 236 | #### Batch Crop 237 | 238 | `mogrify -path outputFolder -auto-orient -crop newWidthxnewHeight+xOrigin+yOrigin *.JPG` 239 | 240 | xOrigin and yOrigin are offset from top left corner of src image. 241 | auto orient will read image metadata to keep them rotated if camera was sideways during shot. 242 | 243 | #### Batch LZW Compression 244 | 245 | `mogrify -path outFolder -depth 16 -endian msb -compress lzw *.tif` 246 | 247 | #### Make grid from folder 248 | 249 | `montage *.jpg -geometry wxh+xSpace+ySpace -tile 6x7 ../montage.jpg` 250 | 251 | xSpace and ySpace determine the gap between tiles, use 0 for no gap 252 | 253 | More info: http://www.imagemagick.org/Usage/montage/ 254 | 255 | ## ufraw 256 | http://ufraw.sourceforge.net/ 257 | 258 | Ufraw is useful if you don't have adobe camera raw or need to convert raw to other file formats. Imagemagick relies on it for some conversion. 259 | 260 | `brew install ufraw` 261 | 262 | #### Batch Conversion 263 | 264 | ```#!/bin/bash 265 | i=0 266 | mkdir converted 267 | for file in ~/Desktop/pics/*.ARW 268 | do 269 | #echo "resized/$i.jpg" 270 | ufraw-batch --output="converted/$i.jpg" "$file" 271 | i=$((i+1)) 272 | done 273 | ``` 274 | 275 | 276 | ## Other References 277 | https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence 278 | 279 | http://www.stuudio.ee/anothergui/presets.html 280 | 281 | https://ffmpeg.org/ffmpeg.html#Main-options 282 | 283 | http://randombio.com/linuxsetup141.html 284 | --------------------------------------------------------------------------------