Some useful FFmpeg commands and scripts.
Snehith,
FFmpeg is a free and open-source framework for handling audio and video files. It supports a wide range of formats and codecs, and offers tools for encoding, decoding, and streaming multimedia files.
FFmpeg supports formats like AV1, VP8, and Opus, ensuring your content is freely usable and FFmpeg’s documentation makes it easy to integrate its functionality into your projects. FFmpeg is licensed under the LGPL license, promoting software freedom.
We start here, by ensuring our content to be free and open source coding format.
For Videos: I prefer Webm and Opus, as they are modern and efficient.
ffmpeg -i input_video.mp4 -c:v libvpx-vp9 -crf 28 -b:v 0 -c:a libopus -vbr on -threads number_of_threads output_video.webm
For Audio: I prefer only Opus, it has best bitrate and latency combinations compared with other audio formats.
ffmpeg -i input_audio.mp4 -c:a libopus -vbr on output_audio.opus
Convert all videos in the directory to WebM,
for i in *.mp4; do ffmpeg -i "$i" -c:v libvpx-vp9 -crf 28 -b:v 0 -c:a libopus -vbr on -threads number_of_threads "${i%.*}.webm"
Now some useful commands,
ffmpeg -i input.mp4 output.avi
Convert input.mp4 to output.avi (basic format change).
ffmpeg -i input.mp4 -vfr output.mp4
Convert with variable frame rate (useful for audio/video sync).
ffmpeg -i input.mp4 -map 0:v -map 0:a output.mkv
Copy all video and audio streams from input to output.
ffmpeg -i input.mp4 -ss 00:01:30 -to 00:02:00 output.mp4
Extract a segment from 1:30 to 2:00.
ffmpeg -i input.mp4 -t 60 output.mp4
Extract the first 60 seconds.
ffplay input.mp4
Play input.mp4 (basic player).
Video Manipulation:
ffmpeg -i input.mp4 -vf scale=640:480 output.mp4
Resize to 640x480.
ffmpeg -i input.mp4 -vf crop=w:h:x:y output.mp4
Crop a region (w=width, h=height, x=left, y=top).
ffmpeg -i input.mp4 -vf "eq=brightness=10" output.mp4
Adjust brightness (example).
ffmpeg -i input.mp4 -vf "setsar=1" output.mp4
Set Sample Aspect Ratio (SAR) to 1.
ffmpeg -i input.mp4 -vf "deinterlace" output.mp4
Deinterlace a video (remove interlacing artifacts).
Audio Manipulation:
ffmpeg -i input.mp4 -vn -acodec copy output.aac
Copy audio stream to AAC (no video).
ffmpeg -i input.mp4 -vn -acodec libmp3lame -ab 128k output.mp3
Encode audio to MP3 at 128kbps.
ffmpeg -i input.mp4 -vn -acodec aac -b:a 128k output.m4a
Encode audio to AAC at 128kbps.
ffmpeg -i input.mp4 -vn -af "volume=2dB" output.mp3
Increase audio volume by 2dB.
Subtitle Handling:
ffmpeg -i input.mp4 -c:s mov_text output.mp4
Burn in subtitles (if present).
ffmpeg -i input.mp4 -map 0:s output.mp4
Copy subtitles from input to output.
Advanced & Optimization:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
Encode video with x264 (good quality/size).
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -preset medium -c:a aac -b:a 128k output.mp4
Encode video with x265 (better compression, slower).
ffmpeg -i input.mp4 -threads 4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
Use 4 threads for faster encoding.
ffmpeg -i input.mp4 -map 0 -c copy output.mp4
Stream copy (fastest, no re-encoding).
ffmpeg -i input.mp4 -an output.mp3
Extract audio only, no video.
Stream Manipulation:
ffmpeg -i input.mp4 -map 0:v:0 -map 0:a:0 output.mkv
Select specific video and audio streams.
ffmpeg -i input.mp4 -sn -an output.mp3
No subtitles, no audio.
ffmpeg -i input.mp4 -f mp3 -ab 128k output.mp3
Force MP3 output.
Combining & Splitting:
ffmpeg -i input.mp4 -f concat -safe 0 -i list.txt -c copy output.mp4- The list.txt file shold have all the file names in this format
file 'file1.mp4'followed by the next file in new line. - we can use the printf comand to make things easier
printf "file '%s'\n" *.mp4 > mylist.txt
- The list.txt file shold have all the file names in this format
Concatenate multiple files from a list.
ffmpeg -i input.mp4 -map 0:0 -to 00:05:00 -c copy output.mp4
Extract a segment and copy the stream.
ffmpeg -i input.mp4 -ss 00:00:00 -to 00:00:10 -c copy output.mp4
Extract a segment from 0:00:00 to 0:00:10.
Note:
There is a ALOT more ffmpeg can do, and this list of commands is all I can remember and used sometime in the past. So this list is not exhaustive and I may add few more in the future.