03. November 2015 · Comments Off on Concatenating Audio Files in Windows with FFmpeg · Categories: Audio, Multimedia · Tags:
This guide demonstrates how to automate concatenating two or more audio files of the same format using FFmpeg while preserving the metadata in the merged file. A tested example batch file is provided to help illustrate. This guide is related to a previous post for concatenating audio files also with a batch file, but with using a different set of command-line tools for joining them.

clapper board

What is FFmpeg?

FFmpeg is an open-source and cross-platform command-line set of SW programs for recording, converting and streaming audio and video. First developed under Linux, compiled versions are available for most operating systems and platforms. It has since become one of the best-known and most widely used products of its type. FFmpeg is compatible with a large number of SW applications and it’s used in many popular SW products such as VLC Media Player, YouTube, and Handbrake. Although FFmpeg includes over 100 codecs and a mind-boggling number of possible combined command-line options, it’s not that difficult to use.

To demonstrate how easy it can be, FFmpeg.org provides this example command for converting input.mp4 to output.avi on its homepage:

$ ffmpeg -i input.mp4 output.avi

While the above example is simple, FFmpeg commands can also be more complex.

Main FFmpeg Tools

The FFmpeg version used for this guide is a Windows complied version availiable from Zeranoe FFmpeg. Simply download the latest 32 or 64-bit static build from the builds page here and extract it to a folder (builds are compressed in 7z format). The version used was git-dee7440 (2015-11-01).

The FFmpeg package contains the following three main executable tools in the bin folder:

  • ffmpeg.exe – a command-line encoder and media converter
  • ffmprobe.exe – a command-line tool to analyze and display media information
  • ffplay.exe – a simple media player

Note that there are a number of commands that are common between the three tools. Only ffmpeg.exe and ffmprobe.exe are used in this guide.

FFmpeg Help Files

FFmpeg provides three levels of help files: basic, long, and full. To view the commands available in the help files, click the ff-prompt.bat file in the root directory to open a command window or open your own in the bin folder and type in one of the following commands:

ffmpeg -h -- print basic options
ffmpeg -h long -- print more options
ffmpeg -h full -- print all options (including all format and codec specific options, very long

Here are links to the basic help file: ffmpeg_help and the full help file: ffmpeg_help_full. When printed to text, the help file sizes are: 5kB for the basic, 25kB for the full, and 487kB for the long help files. The long help file contains about 7,000 lines, which provides a hint as to how many commands are available.

Batch File

Batch file link: tasks_FFmpeg.bat

The link for the batch file used for this guide is provided above. Be sure to remove the txt extension before using it. The batch file is setup to work by the drag and drop method only.

To concatenate the audio files and write the metadata to the merged file, the batch file steps through the following:

  1. Delete any previous metadata text files, listfiles, or json data files.
  2. Write metadata for each audio file to a separate metadata text file
  3. Write the filenames of the audio files to confiles.txt (see below format), a listfile FFmpeg uses to identify the audio files to concatenate
  4. Concatenate the audio files and write the metadata from the first audio file’s metadata.txt file to the joined audio file.

For FFmpeg to use a listfile (confiles.txt), the filenames must be written to the confiles.txt file on separate lines using the following format.

file 'filename1.ext'
file 'filename2.ext'

The provided batch script should work for most audio formats of the same type provided they were encoded using the same codec (although they can be different containers). It was tested with opus, mp3, and aac files on a PC running Windows XP.

The batch script also provides options to separately print out audio file metadata, basic data, or stream and container (format) data to screen or to a file in json format (human readable form), with options to change the format in the script.

Command-Line Description

The main FFmpeg commands for extracting the metadata and concatenating the audio files are explained below. Although the ffprobe command was also used, it’s fairly simple to understand and should be self-explanatory by examining the code and the help files. The filenames used below are not actually in the batch script but have been simplified for explaination purposes.

The below command extracts and writes the metadata to a text file:

ffmpeg -i infile.ext -f ffmetadata metadatafile1.txt
  • ffmpeg – calls ffmpeg
  • -i infile.ext – specifies infile.ext as an input file
  • -f ffmetadata metadatafile1.txt  – formats the metadata to file metadatafile1.txt

The metadata1.txt file is formatted as below:

;FFMETADATA1
album=Name of Album
artist=Artist Name
genre=Music Genre Name
title=Title Name
track=Track #
date=Date
encoder=Encoder Used

The next command is used to process mp3 files only. It concatenates the mp3 files and writes the metadatafile1.txt contents in id3 format of the first mp3 file to the merged mp3 file:

ffmpeg -f concat -i confiles.txt -i metadatafile1.txt -map_metadata 1 -id3v2_version 3 -write_id3v1 1 -c copy filename1_merged.mp3
  • ffmpeg – calls ffmpeg
  • -f concat -i confiles.txt – use concat demuxer to concatenate the mp3 files listed in confiles.txt (treats confiiles.txt as a single file and specifies it as input file 0)
  • -i metadatafile1.txt – specify metadatafile1.txt as input file 1
  • -map_metadata 1 – map input file 1 as the source for metadata (metadatafile1.txt)
  • -id3v2_version 3 – select ID3v2 version 3 to write
  • -write_id3v1 1 – enable ID3v1 writing
  • -c copy filename1_merged.mp3 – stream copy to filename1_merged.mp3 (-c copy = stream copy)

The next command listed processes audio files other than mp3. It functions the same as the previous command but writes standard metadata (if present) from the first audio file to the merged file:

ffmpeg -f concat -i confiles.txt -i metadatafile1.txt -map_metadata 1 -c copy filename1_merged.ext
  • ffmpeg – calls ffmpeg
  • -f concat -i confiles.txt – use concat demuxer to concatenate the files listed in confiles.txt (treats confiiles.txt as a single file and specifies it as input file 0)
  • -i metadatafile1.txt – specify metadatafile1.txt as input file 1
  • -map_metadata 1 – map input file 1 as the source for metadata (metadatafile1.txt)
  • -c copy filename1_merged.mp3 – stream copy to filename1_merged.ext (-c copy = stream copy)

Additional Help – Online Command-line Generators

There are a number of online FFmpeg command-line generators that can help construct various audio and video FFmpeg commands. A few of them are listed below:

FFmpeg Little Helper – Rodrigo Polo

FFmpeg Helper

FFmpeg Command Generator

References:

Useful FFmpeg Commands

Concatenating media files

FFprobeTips

FFmpeg & FFprobe Cheatsheet

 

 

 

Share
Bear

Bad Behavior has blocked 227 access attempts in the last 7 days.

Private