Skip to content

Instantly share code, notes, and snippets.

@amdest
Forked from basperheim/useful-ffmpeg-commands.md
Created August 15, 2024 13:37
Show Gist options
  • Save amdest/1b500e49d7e3ec7bb854e9d924093b19 to your computer and use it in GitHub Desktop.
Save amdest/1b500e49d7e3ec7bb854e9d924093b19 to your computer and use it in GitHub Desktop.
Useful FFmpeg commands

Useful FFmpeg Commands

Remove Metadata from Video File

To remove metadata from a video file using FFmpeg, you can use the following command:

ffmpeg -i input.mp4 -map_metadata -1 -c:v copy -c:a copy output.mp4

Convert Video to Different Format

To convert a video file to a different format using FFmpeg, you can use the following command:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4

Extract Audio from Video

To extract the audio from a video file using FFmpeg, you can use the following command:

ffmpeg -i input.mp4 -vn -c:a copy output.mp3

This command disables video processing (-vn) and copies the audio stream without any re-encoding, creating an output file in MP3 format.

Concatenate Video Files

To concatenate multiple video files into a single file using FFmpeg, you can use the following command:

ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4

This command reads the list of input files from a text file (filelist.txt) and concatenates them into a single output file without any re-encoding.

Convert AVI to MPEG4

Is it worth it to convert AVI to MP4? If so, how can I do this using an FFmpeg command?

Converting AVI to MP4 can be beneficial if you want to improve compatibility, reduce file size, or utilize additional features supported by the MP4 format. FFmpeg, a powerful command-line tool for manipulating multimedia files, can be used to convert AVI to MP4. Here's an example FFmpeg command for converting AVI to MP4:

ffmpeg -i input.avi -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k output.mp4

Let's break down the command:

Breakdown of the FFmpeg AVI conversion command

ffmpeg: Invokes the FFmpeg tool.

  • -i input.avi: Specifies the input AVI file.

  • -c:v libx264: Sets the video codec to libx264, which is an efficient H.264 video codec commonly used in MP4 files.

  • -preset medium: Specifies the encoding preset. Options include ultrafast, superfast, veryfast, faster, fast, medium (default), slow, slower, veryslow, and placebo. The preset determines the trade-off between encoding speed and file size. You can adjust this parameter based on your preference.

  • -crf 23: Sets the Constant Rate Factor (CRF) value for video quality. Lower values (e.g., 18-23) result in higher video quality and larger file sizes, while higher values (e.g., 24-51) decrease video quality and produce smaller file sizes. You can adjust this value to balance quality and file size according to your needs.

  • -c:a aac -b:a 128k: Specifies the audio codec as AAC with a target bitrate of 128k. This ensures good audio quality in the resulting MP4 file. You can modify the bitrate as desired.

  • output.mp4: Sets the output file name and format (MP4).

Make sure you have FFmpeg installed on your system before using the command. Additionally, feel free to adjust the command parameters according to your specific requirements, such as changing the video codec, preset, CRF value, or audio settings.

Remember to replace "input.avi" with the name of your input AVI file and "output.mp4" with your desired output file name.

Trim audio file

To trim an MP3 file using FFmpeg without corrupting it, you can use the -ss (start time) and -t (duration) options.

Here's the basic syntax:

ffmpeg -i input.mp3 -ss start_time -t duration -c:a copy output.mp3
  • Replace input.mp3 with the name of your input MP3 file.
  • start_time is the starting point of the trim, specified in seconds or in the format hh:mm:ss.
  • duration is the duration of the trimmed portion, also in seconds or hh:mm:ss.
  • -c:a copy ensures that the audio codec remains the same, which is essential for not corrupting the file.
  • output.mp3 is the name you want to give to the trimmed output file.

For example, if you want to trim the MP3 starting from 30 seconds into the file and lasting for 60 seconds, you would use:

ffmpeg -y -i input.mp3 -ss 00:00:30 -t 00:01:00 -c:a copy output.mp3

This command will create a new MP3 file named output.mp3 containing the trimmed audio.

Make sure to replace input.mp3 with the actual file name and adjust the start_time and duration according to your trimming needs. The -c:a copy option ensures that only the specified portion of the audio is copied to the output file without re-encoding, preserving the audio quality.

Use FFmpeg to increase audio volume without reducing quality

When you use FFmpeg to apply audio filters like volume adjustments, it may not automatically preserve the original audio bitrate for certain output formats.

To maintain the original bitrate while applying audio filters, you can explicitly specify the audio codec and bitrate for the output file. Here's an example command for increasing the volume of an MP3 file while maintaining the original bitrate and audio codec:

ffmpeg -i input.mp4 -filter:a "volume=2.0" output.mp4

To change reencode the to audio to libmp3lame use this command:

ffmpeg -y -i input.mp3 -filter:a "volume=2.0" -c:a libmp3lame -b:a 192k output.mp3

In this command:

  • -i input.mp3: Specifies the input MP3 file.
  • -filter:a "volume=2.0": Applies a volume adjustment filter, doubling the volume (you can adjust the value as needed).
  • -c:a libmp3lame: Specifies the audio codec to be used for the output file (in this case, MP3 using the LAME codec).
  • -b:a 192k: Sets the bitrate for the audio stream to 192 kbps (you can adjust this value to match the original bitrate if needed).
  • output.mp3: Specifies the name of the output file.

By explicitly specifying the audio codec (-c:a libmp3lame) and bitrate (-b:a 192k in this example), you can control the output audio quality and maintain the desired bitrate while applying the volume adjustment filter. Adjust the -b:a value to match the original bitrate of your input MP3 file if necessary.

FFmpeg bash functions

Use FFmpeg to reduce the size of an MP4 without greatly sacrificing quality

Example usage: ffmpeg_reduce path/to/input.mp4 4 (-threads arg is optional):

ffmpeg_reduce() {
  local input_file="$1"
  local threads_arg="$2"

  # Check if the input file exists
  if [ ! -f "$input_file" ]; then
    echo "Error: Input file '$input_file' not found or is not a regular file."
    return 1
  fi

  # Check if the input file has a valid MP4 file extension
  if [[ "$input_file" != *.mp4 ]]; then
    echo "Error: Input file must have an '.mp4' extension."
    return 1
  fi

  # Generate the output file name with '-reduced'
  local output_file="${input_file%.mp4}-reduced.mp4"

  # Build the FFmpeg command with optional threads argument
  local ffmpeg_command="ffmpeg -y -hide_banner -i \"$input_file\" -vf \"scale=1024:576:force_original_aspect_ratio=decrease\" -c:v libx264 -crf 28 -preset fast -c:a aac -b:a 128k"

  # Add threads argument if provided
  if [ -n "$threads_arg" ]; then
    ffmpeg_command="$ffmpeg_command -threads $threads_arg"
  fi

  # Add output file
  ffmpeg_command="$ffmpeg_command \"$output_file\""

  # Run the FFmpeg command
  eval "$ffmpeg_command"

  echo "Video reduced and saved as '$output_file'."
}

Use FFmpeg to trim an audio/video file

Example usage: ffmpeg_trim path/to/input.mp4 00:01:00 00:02:00:

# Convert HH:MM:SS or HH:MM:SS.mmm to seconds
time_to_seconds() {
    local time_str=$1
    local IFS=':.'
    read -r hours minutes seconds milliseconds <<< "$time_str"

    # If milliseconds are not provided, set them to zero
    milliseconds=${milliseconds:-0}

    # Convert to total seconds
    echo $((10#$hours * 3600 + 10#$minutes * 60 + 10#$seconds)).$milliseconds
}

ffmpeg_trim() {
    local file_path=$1
    local start_time=$2
    local end_time=$3

    local start_sec=$(time_to_seconds "$start_time")
    local end_sec=$(time_to_seconds "$end_time")

    # Calculate duration as the difference between end and start times
    local duration_sec=$(bc <<< "$end_sec - $start_sec")

    local output="${file_path%.*}-trimmed-${start_time//[:.]/-}-to-${end_time//[:.]/-}.${file_path##*.}"

    ffmpeg -y -ss "$start_sec" -i "$file_path" -t "$duration_sec" "$output"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment