computers:ffmpeg

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
computers:ffmpeg [2023/02/23 02:36] – [Useful Commands & Scripts] joncomputers:ffmpeg [2025/12/15 16:40] (current) jon
Line 36: Line 36:
  
 ==== Scripting ==== ==== Scripting ====
 +
 +=== Bash Scripting ===
 +When building out bash scripting, you will need to run your ffmpeg commands with -nostdin if you are piping in variables with paths((https://stackoverflow.com/questions/21634088/execute-ffmpeg-command-in-a-loop)). ffmpeg uses stdin for its commands, which will break your 'while read' commands.
 +
 +<note tip>
 +-stdin
 +
 +Enable interaction on standard input. On by default unless standard input is used as an input. To explicitly disable interaction you need to specify -nostdin.
 +Disabling interaction on standard input is useful, for example, if ffmpeg is in the background process group. Roughly the same result can be achieved with ffmpeg ... < /dev/null but it requires a shell.</note>
  
 === Batch Filing === === Batch Filing ===
Line 64: Line 73:
  
 === Powershell Coding === === Powershell Coding ===
 +
 +==== Frequent Commands ====
 +
 +https://www.videoproc.com/resource/ffmpeg-commands.htm
 +
 +==== Listing out features ====
 +
 +https://dev.to/drsensor/list-of-helpful-ffmpeg-command-for-checking-capabilities-1110
  
 ==== Watermarking ==== ==== Watermarking ====
Line 81: Line 98:
 Example using Powershell: Example using Powershell:
 <code>ffmpeg -ss 00:53:00 -i 'input' -avoid_negative_ts 1 -to 07:19:17 -c copy 'output'</code> <code>ffmpeg -ss 00:53:00 -i 'input' -avoid_negative_ts 1 -to 07:19:17 -c copy 'output'</code>
 +
 +=== Caveats to cropping ===
 +
 +When you do cut with -c copy (no re-encode), and the cut point is not exactly on a keyframe, and/or the video uses B-frames (re-ordered frames), ffmpeg can’t rewrite frames to make a clean boundary. It copies packets but the decode/display timestamps (DTS/PTS) can become inconsistent around the cut boundary. Then when you concat, ffmpeg sees timestamps going “backwards” and prints: "Non-monotonic DTS... This may result in incorrect timestamps."
 +
 +Players often still play linearly, but seeking near the splice can fail hard (especially around the crop point). mp4 is particularly sensitive because it stores timing/indexing in a way that makes sloppy cuts more painful than in some other workflows.
 +
 +Workaround is to use a copy-cut and regenerate timestamps
 +
 +<code>ffmpeg -ss 0 -to 02:03:12 -i ogvideo.mp4 \
 +  -map 0 -c copy \
 +  -fflags +genpts -avoid_negative_ts make_zero \
 +  croppedvid.mp4</code>
 +
 +Then, "remux normalize" the video to give clean metadata. This is a nice to do on all the videos that you're working on if concat-ing them togther
 +
 +<code>
 +ffmpeg -i croppedvid.mp4 -map 0 -c copy -movflags +faststart fixed1.mp4
 +</code>
 +
 ==== Joining video files together (Concatenate) ==== ==== Joining video files together (Concatenate) ====
  
Line 86: Line 123:
 https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg
 https://stackoverflow.com/questions/18474312/concatenate-join-mp4-files-using-ffmpeg-and-windows-command-line-batch-not-lin https://stackoverflow.com/questions/18474312/concatenate-join-mp4-files-using-ffmpeg-and-windows-command-line-batch-not-lin
 +
 +Make a text file in the same directory. Each video needs the word 'file' at the beginning of the line to delineate.
 +<code>
 +file videofile.mp4
 +file videfile2.mp4
 +</code>
 +
 +Here's how I've scripted this. Note, this pertains more to macOS with the sed statement since the single quotes are needed, but the tweak for linux is to just remove the single quotes and the -e
 +<code>
 +find . -type f > concat.txt && sed -i '' -e '/^\.\/\.DS_Store$/d' -e '/^\.\/concat\.txt$/d' -e 's/^\.\///' -e 's/^/file /' concat.txt
 +</code>
 +
 +Then, reference the text file as your input.
 +<code>
 +ffmpeg -f concat -safe 0 -i concat.txt -c copy output.mp4
 +</code>
  
 ==== Splitting a large video into smaller clips ==== ==== Splitting a large video into smaller clips ====
 +https://trac.ffmpeg.org/wiki/Seeking#Cuttingsmallsections
  
 Example in Batch: Example in Batch:
Line 140: Line 194:
  
 Trying to adjust MKV files. Safe bet, do all adjustments to MP4 file first, then MKV when permanent. - https://superuser.com/questions/1411133/ffmpeg-invalid-length-0x59f0-0x8bd7fe90-in-parent-when-trying-to-do-anything Trying to adjust MKV files. Safe bet, do all adjustments to MP4 file first, then MKV when permanent. - https://superuser.com/questions/1411133/ffmpeg-invalid-length-0x59f0-0x8bd7fe90-in-parent-when-trying-to-do-anything
 +
 +==== Limiting Cores/Threads ====
 +
 +https://superuser.com/questions/155305/how-many-threads-does-ffmpeg-use-by-default
 +
 +
 +==== Hardware Acceleration ====
 +
 +https://trac.ffmpeg.org/wiki/HWAccelIntro
 +https://www.rickmakes.com/ffmpeg-notes/
 +
 +Each OS has its own Platform APIs of hardware acceleration and additionally, Intel, AMD, and Nvidia GPUs have their own hardware acceleration protocols.
 +
 +Nvidia on Windows/Linux has h264_nvenc, hevc_nvenc(h265), and av1_nvenc
 +Intel on Windows/Linux has h264_qsv, hevc_qsv, etc...
 +
 +macOS supports VideoToolBox for Intel QuickSync, such as h264_videotoolbox, hevc_videotoolbox, and prores_videotoolbox 
  
 ==== Unorganized Links ==== ==== Unorganized Links ====
  • computers/ffmpeg.1677119767.txt.gz
  • Last modified: 2023/02/23 02:36
  • by jon