User Tools

Site Tools


ffmpeg

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ffmpeg [2020/10/30 05:10] – [See Also] tags rjtffmpeg [2023/08/27 20:51] (current) – FFmpeg Explorer rjt
Line 1: Line 1:
 ====== FFmpeg ====== ====== FFmpeg ======
 +
 +Super handy [[terminal_emulator|terminal]]-based tool for working with video.
  
 ===== Use ===== ===== Use =====
Line 7: Line 9:
 Most of the time I just use--- Most of the time I just use---
  
-<code>ffmpeg -i input.ext output.ext</code>+<code>ffmpeg -i [input.ext] [output.ext]</code>
  
 ---and it usually works fine leaving it to just use defaults. ---and it usually works fine leaving it to just use defaults.
 +
 +This gives good results (and a smaller file size) converting MP3 to OGG though:
 +
 +<code>ffmpeg -i [input].mp3 -c:a libvorbis -q:a 4 [output].ogg</code>
 +
 +Fuck-off those .WEBMs:
 +
 +<code>ffmpeg -i [input].webm -acodec copy -vcodec copy [output].mkv</code>
  
 === Multiple === === Multiple ===
Line 15: Line 25:
 For doing a whole directory I use this one-liner I got from Stack Exchange. For doing a whole directory I use this one-liner I got from Stack Exchange.
  
-<code>for i in *.ext; do ffmpeg -i "$i" "${i%.*}.ext"; done</code>+<code>for i in *.[ext]; do ffmpeg -i "$i" "${i%.*}.[ext]"; done</code> 
 + 
 +For converting files for my [[m-u-s-u-c]] blog I also strip out the cover images: ''-map a'' tells it to take only the audio stream. 
 + 
 +<code>for i in *.mp3; do ffmpeg -i "$i" -c:a libvorbis -q:a 4 -map a "${i%.*}.ogg"; done</code>
  
 You can use the wildcard ''*'' in the file extension to, so if you've got a mix of .mkv and .mp4 files from Youtube you can do. say: You can use the wildcard ''*'' in the file extension to, so if you've got a mix of .mkv and .mp4 files from Youtube you can do. say:
Line 23: Line 37:
 If you want to put the new videos into a subdirectory, first create it, then simply add it to the output part: If you want to put the new videos into a subdirectory, first create it, then simply add it to the output part:
  
-<code>for i in *.ext; do ffmpeg -i "$i" "directory/${i%.*}.ext"; done</code>+<code>for i in *.[ext]; do ffmpeg -i "$i" "directory/${i%.*}.[ext]"; done</code> 
 + 
 +For converting a directory of .FLAC files to .MP3. ''[-q:a 0|-b:a 320k]'' is your choice between VBR with the best quality (0) or a CBR of 320k. VBR is generally just as good, though sometimes you need CBR. 
 + 
 +<code>for i in *.flac; do ffmpeg -i "$i" [-q:a 0|-b:a 320k] "${i%*}.mp3"; done</code>
  
 ==== Editing ==== ==== Editing ====
Line 35: Line 53:
 <code>ffmpeg -f concat -safe 0 -i "inputfile.txt" -c copy "inputfile.ext"</code> <code>ffmpeg -f concat -safe 0 -i "inputfile.txt" -c copy "inputfile.ext"</code>
  
-nbjp Todouse bash to generate the file list+[[https://trac.ffmpeg.org/wiki/Concatenate#demuxer|FFmpeg's docs on generating the list]]: 
 + 
 +<code> 
 +# with a bash for loop 
 +for f in *.wav; do echo "file '$f'" >> mylist.txt; done 
 +# or with printf 
 +printf "file '%s'\n" *.wav > mylist.txt 
 +</code> 
 + 
 +If you have a movie in two parts and want to stick them together with less fuss, or you just want to save yourself the trouble of making the file, you can use //process substitution//: 
 + 
 +<code>ffmpeg -f concat -safe 0 -i <(for f in ./*.avi; do echo "file '$PWD/$f'"; done) -c copy output.avi</code> 
 + 
 + 
 +NB: Some older codecs let you stick files together with just [[cat]]!: https://ffmpeg.org/faq.html#toc-Concatenating-using-the-concat-protocol-_0028file-level_0029 
 + 
 +=== Cropping === 
 +To crop a 16:9 video to 4:3, add: 
 + 
 +<code>-filter:v "crop=ih/3*4:ih"</code> 
 + 
 +Full example, with copied audio stream: 
 + 
 +<code>ffmpeg -i [file] -filter:v "crop=ih/3*4:ih" -c:a copy [file]</code>
  
 === Remove Audio === === Remove Audio ===
Line 46: Line 87:
  
 === Trimming === === Trimming ===
-You can pretty easily make a video file smaller. The only caveat is that if you don't want to re-encode you're limited by where keyframes are placed.+You can pretty easily make a video shorter. The only caveat is that if you don't want to re-encode you're limited by where keyframes are placed.
  
 <code>ffmpeg -i inputfile.ext -ss HH:MM:SS -to HH:MM:SS -c copy outputfile.ext</code> <code>ffmpeg -i inputfile.ext -ss HH:MM:SS -to HH:MM:SS -c copy outputfile.ext</code>
Line 58: Line 99:
   * If you leave out the ''-c copy'' it will re-encode the video   * If you leave out the ''-c copy'' it will re-encode the video
  
 +==== Images ====
 +=== Extract each frame as an image ===
 +<code>ffmpeg -i [file] frame%03d.png</code>
 +
 +=== Create video from images ===
 +<code>cat *.png | ffmpeg -framerate [framerate] -f image2pipe -i - [file]</code>
 +
 +== GIF ==
 +Default loops. Add ''-loop -1'' to only play once.
 +
 +<code>cat *.png | ffmpeg -framerate [framerate] -f image2pipe -i - [filename].gif</code>
 +
 +== APNG ==
 +Default doesn't loop. Add ''-plays 0'' to loop.
 +
 +<code>cat *.png | ffmpeg -framerate [framerate] -f image2pipe -i - -f apng [filename].png</code>
 ==== Lowering the file size ==== ==== Lowering the file size ====
 Slow, but compresses better: Slow, but compresses better:
Line 72: Line 129:
  
   * https://trac.ffmpeg.org/wiki/Scaling   * https://trac.ffmpeg.org/wiki/Scaling
 +
 +==== Repairing ====
 +
 +Just had an .AVI file throwing error messages about a busted index. Copying the file seemed to fix it.
 +<code>ffmpeg -i "inputfile.avi" -c:v copy -c:a copy "outputfile.avi"</code>
 +
 +===== Frontends =====
 +
 +  * [[https://ffmpeg.lav.io/|FFmpeg Explorer]] - Browser-based GUI with a node-based interface for changing parameters.
  
 ===== See Also ===== ===== See Also =====
   * [[https://www.ffmpeg.org/|www.ffmpeg.org]]   * [[https://www.ffmpeg.org/|www.ffmpeg.org]]
 +  * [[https://trac.ffmpeg.org/wiki|FFmpeg Bug Tracker and Wiki]]
 +  * [[gifsicle]] - Similar tool, specifically for working with [[gif|GIFs]]
  
 {{tag>CLI guide software tool video}} {{tag>CLI guide software tool video}}
ffmpeg.1603995046.txt.gz · Last modified: 2020/10/30 05:10 by rjt