FFmpeg usage examples (Docker or on Ubuntu)

FFmpeg is an open-source multimedia framework and software suite that provides libraries and command-line tools for decoding, encoding, converting, and processing audio, video, and other multimedia data. It supports various tasks, including format conversion, transcoding, video scaling, audio and video filtering, streaming, and basic editing. With cross-platform availability and extensive capabilities, FFmpeg is essential for multimedia professionals, developers, and enthusiasts who work with media files and need to manipulate, convert, or process them across various formats and codecs.

You can get the FFmpeg from the Docker Hub:

Example Dockerfile

# Use an official Debian-based runtime as a parent image
FROM debian:bullseye-slim

# Set the working directory inside the container
WORKDIR /app

# Install FFmpeg and other dependencies
RUN apt-get update && \
    apt-get install -y ffmpeg && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set the entry point
ENTRYPOINT ["ffmpeg"]

FFmpeg usage examples

To use FFmpeg from the command line, open a terminal or command prompt on your computer and then enter the appropriate FFmpeg command followed by the necessary options and arguments. Here are some common examples of how to use FFmpeg from the command line:

  1. Convert Video Format:
   ffmpeg -i input.mp4 output.avi

This command converts a video file from input.mp4 to output.avi`.

  1. Resize Video:
   ffmpeg -i input.mp4 -vf scale=640:480 output.mp4

This command resizes a video to dimensions 640×480.

  1. Extract Audio:
   ffmpeg -i input.mp4 -q:a 0 -map a output.mp3

This command extracts the audio from a video file as an MP3.

  1. Concatenate Videos:
   ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4

This command concatenates two video files (input1.mp4 and input2.mp4) into one output video.

  1. Convert Audio Format:
   ffmpeg -i input.wav -c:a libmp3lame output.mp3

This command converts an audio file from WAV to MP3.

  1. Add Watermark:
   ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=W-w-10:H-h-10" output.mp4

This command adds a watermark (watermark.png) to a video.

  1. Cut Video:
   ffmpeg -i input.mp4 -ss 00:00:10 -to 00:00:20 -c:v copy -c:a copy output.mp4

This command cuts a segment from a video starting at 10 seconds and ending at 20 seconds.

  1. Extract Frames as Images:
   ffmpeg -i input.mp4 -vf "fps=1" frames/frame-%d.png

This command extracts frames from a video at a rate of 1 frame per second and saves them as images.

FFmpeg for streaming via RTMP (Real-Time Messaging Protocol)

You can use the following command line template as a starting point to use FFmpeg for streaming via RTMP (Real-Time Messaging Protocol). Replace placeholders like input_file, stream_key, and rtmp_url with your specific details:

ffmpeg -i input_file -c:v libx264 -pix_fmt yuv420p -preset veryfast -g 30 -b:v 2500k -c:a aac -b:a 128k -f flv rtmp://rtmp_url/stream_key

Here’s an explanation of the command and its options:

  • -i input_file: Specifies the input media file.
  • -c:v libx264: Sets the video codec to H.264.
  • -pix_fmt yuv420p: Specifies the pixel format for video.
  • -preset very fast: Specifies the encoding speed preset (adjust based on your needs).
  • -g 30: Sets the GOP (Group of Pictures) size to 30 frames.
  • -b:v 2500k: Sets the video bitrate to 2500 kbps.
  • -c:a aac: Sets the audio codec to AAC.
  • -b:a 128k: Sets the audio bitrate to 128 kbps.
  • -f flv: Specifies the output format as FLV.
  • rtmp://rtmp_url/stream_key: Specify the RTMP URL and stream key where you want to stream.

Replace input_file with the path to your source media file (e.g., a video file), rtmp_url with the RTMP server URL, and stream_key with the stream key provided by your streaming platform.

For example, if you’re streaming to Twitch, the RTMP URL might look like rtmp://live.twitch.tv/app`, and the stream key would be your unique Twitch stream key.

Remember that FFmpeg and RTMP streaming requires a stable internet connection and appropriate hardware resources. You may also need to adjust the encoding settings based on your available bandwidth and desired stream quality. Some streaming platforms might require specific settings or authentication methods, so refer to their documentation for any platform-specific instructions.

These are just a few examples of what you can do with FFmpeg from the command line. FFmpeg has many options and capabilities, so you’ll want to refer to the FFmpeg documentation and manuals for more detailed information on its usage and available features. When using FFmpeg, specify input and output file paths, codec options, filters, and other parameters as needed for your specific task.