ffmpeg is a powerful tool to process video and audio files. Let's create a cheat sheet of the commands I use from time to time!
#Remove audio from a video
Shell
ffmpeg -i input.mp4 -an -c:v copy output.mp4
-i input.mp4: Input file-an: Remove audio stream-c:v copy: Copy the video stream without re-encoding
#Prepare a video for streaming
Remove metadata and move the moov atom to the beginning of the file for faster playback start.
Shell
ffmpeg -i input.mp4 -map_metadata -1 -movflags faststart output.mp4
-i input.mp4: Input file-map_metadata -1: Remove all metadata-movflags faststart: Move the moov atom to the beginning of the file
#Encode a video by chunks
PowerShell
# Split the video into 10-minute chunks and remove audio
ffmpeg -i input.mp4 -c copy -an -map 0 -segment_time 600 -f segment chunk%03d.mp4
foreach ($file in Get-ChildItem -Filter "chunk*.mp4") {
# Encode each chunk
ffmpeg -i $file.FullName -c:v libx265 -preset medium -c:a aac -b:a 128k converted$($file.BaseName).mp4
}
# Concatenate the chunks into a single video
foreach($file in Get-ChildItem -Filter "converted*.mp4") {
"file '$($file.FullName)'" | Out-File -Append -Encoding utf8 concat.txt
}
ffmpeg -f concat -safe 1 -i concat.txt -c copy concat.mp4
# Merge video and audio
ffmpeg -i concat.mp4 -i input.mp4 -c copy -map 0:v:0 -map 1:a:0 converted.mp4
# Remove temporary files
Remove-Item concat.txt
Remove-Item chunk*.mp4
Remove-Item converted*.mp4
-segment_time 600: Split the video into chunks of 600 seconds (10 minutes)-f segment: Use the segment muxer to split the video-c:v libx265: Encode the video using the H.265 codec-preset medium: Use the medium preset for encoding (balance between speed and compression)-c:a aac: Encode the audio using the AAC codec-b:a 128k: Set the audio bitrate to 128 kbps-f concat: Use the concat demuxer to merge the chunks-safe 1: Prevent the use of unsafe file paths in the concat file-map 0:v:0: Select the first video stream from the first input file-map 1:a:0: Select the first audio stream from the second input file
#Merge video and subtitles
Shell
ffmpeg -i input.mp4 -i subtitles.srt -c copy -c:s mov_text output.mp4
-i input.mp4: Input video file-i subtitles.srt: Input subtitle file-c copy: Copy video and audio streams-c:s mov_text: Convert subtitles to mov_text format (required for MP4 container)
Do you have a question or a suggestion about this post? Contact me!