FFmpeg – examples and convenience lines

# extract one frame at the end of a video
ffmpeg -sseof -0.1 -i intro_1.mp4 -frames:v 1 -q:v 1 intro_end.jpg

-sseof -0.1: This option tells FFmpeg to seek to 0.1 seconds before the end of the file. This approach is often more reliable for extracting the last frame, especially if the video’s duration isn’t an exact multiple of the frame interval.
Super User
-frames:v 1: Extracts a single frame.
-q:v 1: Sets the quality of the output image; 1 is the highest quality.

# extract one frame at the beginning of a video
ffmpeg -i speaking_4.mp4 -frames:v 1 speaking_beginning.jpg

# check video length
ffmpeg -i C:\myvideo.mp4 -f null –

# Convert mov/mp4 to animated gifEdit
ffmpeg -i input.mp4 -pix_fmt rgb24 output.gif
Other useful ffmpeg commandsEdit

# split a long video in two parts by slicing it at 30 minutes
# check the duration of the video first, to make sure it is longer than 30 minutes
ffmpeg -i output1.mp4  | & grep Dura
# split the first 30 minutes
ffmpeg -i original_video.mp4 -vcodec copy -acodec copy -t 00:30:00 output_1.mp4
# split from 30 minutes to the end
ffmpeg -i original_video.mp4 -vcodec copy -acodec copy -ss 00:30:00 output_2.mp4

# check frame count
ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 input.mp4

# combine sound and video
ffmpeg -i sound.mp3 -i video.mp4 -f mp4 -acodec copy -vcodec copy output.mp4

# combine multiple videos
create a text file with file paths, eg: fileList.txt
file ‘/home/file1.mp4’
file ‘/home/file1.mp4’
file ‘/home/file3.mp4’

# Then
ffmpeg -f concat -safe 0 -i fileList.txt -c copy mergedVideo.mp4

# convert mp4 to pngs
ffmpeg -i input.mp4  outputOcean.%04d.png

# Compress video
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset medium output.mp4
-c:v libx264: Sets the video codec to H.264 (libx264). This is a widely compatible and efficient codec for MP4. 
-crf 28: Sets the Constant Rate Factor. This value controls the quality-to-file-size ratio.
The range for CRF is typically 0-51, where 0 is lossless (largest file size) and 51 is the lowest quality.
A common range for good quality with reduced size is 18-28. Higher values result in smaller files but lower quality. Experiment to find an acceptable balance.
-preset medium: Controls the encoding speed and compression efficiency.
Slower presets (e.g., slowslowerveryslowplacebo) offer better compression and smaller file sizes at the cost of longer encoding times.
Faster presets (e.g., fastfasterultrafast) encode quicker but result in larger files.
Medium is a good balance between speed and efficiency.

# Resizing: To further reduce file size, especially for high-resolution videos, you can resize the video:
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset medium -vf scale=1280:-1 output_resized.mp4
-vf scale=1280:-1: Resizes the video to a width of 1280 pixels, automatically calculating the appropriate height to maintain the aspect ratio.

# Audio Compression: While the primary focus is video, you can also compress the audio:
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset medium -c:a aac -b:a 128k output.mp4
-c:a aac: Sets the audio codec to AAC.
-b:a 128k: Sets the audio bitrate to 128 kbps. Adjust this value to control audio quality and size.