Using the ffmpeg Command for Video Conversion (with examples)

Using the ffmpeg Command for Video Conversion (with examples)

1: Extracting Sound from a Video and Saving it as MP3

ffmpeg -i video.mp4 -vn sound.mp3

Motivation: Extracting audio from a video can be useful for creating podcasts, music compilations, or simply listening to a video’s sound without the need for visuals.

Explanation: This command uses the -i flag to specify the input file (video.mp4) and the -vn flag to indicate that we only want the audio without the video. The output file (sound.mp3) is specified without any additional flags, implying that ffmpeg should infer the desired output format based on the file extension.

Example Output: The command extracts the audio from the video.mp4 file and saves it as sound.mp3 in the same directory.

2: Saving a Video as a GIF with Scaling and Framerate Settings

ffmpeg -i video.mp4 -vf 'scale=-1:1000' -r 15 output.gif

Motivation: Converting videos to GIF formats can be beneficial for sharing short video clips or animations without requiring the recipient to have a video player.

Explanation: This command uses the -i flag to specify the input file (video.mp4). The -vf flag with the 'scale=-1:1000' argument scales the video to a height of 1000 pixels while maintaining the aspect ratio. The -r flag with the value 15 sets the desired framerate to 15 frames per second. The output file is specified as output.gif.

Example Output: The command generates a GIF file (output.gif) by extracting frames from the video.mp4 file, scaling them to a height of 1000 pixels, and setting the framerate to 15 frames per second.

3: Combining Numbered Images into a Video or GIF

ffmpeg -i frame_%d.jpg -f image2 video.mpg|video.gif

Motivation: Creating a video or GIF from a sequence of numbered images is useful for animating images, creating timelapses, or combining different frames into a video format.

Explanation: This command uses the -i flag to specify the input file pattern (frame_%d.jpg), where %d represents a wildcard for the frame numbers. The -f flag with the image2 argument specifies that the input format is a series of images. The output file (video.mpg or video.gif) format is inferred based on the file extension.

Example Output: The command combines the sequentially numbered images (frame_1.jpg, frame_2.jpg, etc.) into either a video file (video.mpg) or a GIF file (video.gif), depending on the specified output format.

4: Extracting a Single Frame from a Video and Saving it as an Image

ffmpeg -ss mm:ss -i video.mp4 -frames 1 -s 128x128 -f image2 image.png

Motivation: Extracting individual frames from a video is useful for generating thumbnail images, capturing still images from video content, or analyzing specific frames for visual analysis.

Explanation: This command uses the -ss flag with the mm:ss argument to specify the desired time offset from the beginning of the video. The -i flag specifies the input file (video.mp4). The -frames 1 flag indicates that only a single frame should be extracted. The -s flag with the 128x128 argument sets the desired resolution of the output image. The output file is specified as image.png.

Example Output: The command extracts a single frame from the video.mp4 file at the specified time offset (mm:ss), resizes it to a resolution of 128x128 pixels, and saves it as image.png.

5: Trimming a Video from a Start Time to an End Time

ffmpeg -ss mm:ss -to mm2:ss2 -i video.mp4 -codec copy output.mp4

Motivation: Trimming a video allows you to remove unwanted sections or extract specific segments from a longer video, reducing file size and focusing on the desired content.

Explanation: This command uses the -ss flag with the mm:ss argument to specify the desired starting time offset. The -to flag with the mm2:ss2 argument sets the end time. The -i flag specifies the input file (video.mp4). The -codec copy flag ensures that no re-encoding occurs, maintaining the same audio and video streams. The output file is specified as output.mp4.

Example Output: The command trims the video.mp4 file from the specified starting time (mm:ss) to the specified end time (mm2:ss2) and creates a new video file (output.mp4) without re-encoding the video or audio streams.

6: Converting an AVI Video to MP4 with Specific Audio and Video Codecs

ffmpeg -i input_video.avi -codec:a aac -b:a 128k -codec:v libx264 -crf 23 output_video.mp4

Motivation: Converting videos to different formats can be necessary for compatibility with specific devices, web platforms, or efficient storage.

Explanation: This command uses the -i flag to specify the input file (input_video.avi). The -codec:a aac -b:a 128k arguments select the AAC audio codec and set the audio bitrate to 128 kbps. The -codec:v libx264 -crf 23 arguments select the H.264 video codec and set the Constant Rate Factor (CRF) to 23 for a balance of quality and file size. The output file is specified as output_video.mp4.

Example Output: The command converts the input_video.avi file to the MP4 format, encoding the audio using AAC at 128 kbps and the video using H.264 with a CRF of 23, and saves it as output_video.mp4.

7: Remuxing an MKV Video to MP4 Without Re-encoding Audio or Video Streams

ffmpeg -i input_video.mkv -codec copy output_video.mp4

Motivation: Remuxing a video allows you to change the container format (e.g., from MKV to MP4) without re-encoding the audio or video streams, preserving the original quality.

Explanation: This command uses the -i flag to specify the input file (input_video.mkv). The -codec copy argument instructs ffmpeg to copy the audio and video streams from the input file without re-encoding them. The output file is specified as output_video.mp4.

Example Output: The command remuxes the input_video.mkv file by changing the container format to MP4 while preserving the original audio and video streams, resulting in the output_video.mp4 file.

8: Converting an MP4 Video to VP9 Codec for High-Quality Web Streaming

ffmpeg -i input_video.mp4 -codec:v libvpx-vp9 -crf 30 -b:v 0 -codec:a libopus -vbr on -threads number_of_threads output_video.webm

Motivation: Converting videos to the VP9 codec is beneficial for high-quality web streaming, as it offers excellent compression efficiency and broader browser compatibility.

Explanation: This command uses the -i flag to specify the input file (input_video.mp4). The -codec:v libvpx-vp9 argument selects the VP9 video codec. The -crf 30 -b:v 0 arguments set the Constant Rate Factor (CRF) to 30 (lower values for higher quality) and disable the video bitrate. The -codec:a libopus -vbr on arguments select the Opus audio codec with variable bitrate. The -threads number_of_threads argument specifies the number of threads to use during encoding (replace number_of_threads with the desired value). The output file is specified as output_video.webm.

Example Output: The command converts the input_video.mp4 file to the WebM format using the VP9 video codec at a CRF of 30, Opus audio codec with variable bitrate, and a specified number of threads. The resulting video file is saved as output_video.webm.

Related Posts

Using WeasyPrint for HTML to PDF Conversion (with examples)

Using WeasyPrint for HTML to PDF Conversion (with examples)

WeasyPrint is a powerful command-line tool that allows users to convert HTML files to PDF or PNG format.

Read More
How to use the command bitcoin-cli (with examples)

How to use the command bitcoin-cli (with examples)

Bitcoin-cli is a command-line client that allows users to interact with the Bitcoin daemon via remote procedure call (RPC) calls.

Read More
How to use the command protontricks (with examples)

How to use the command protontricks (with examples)

Protontricks is a simple wrapper that allows you to run Winetricks commands for Proton enabled games.

Read More