Skip to content
GitHub

Transcoding Files

When testing files sometimes we want to make sure that the formatting is consistent with our internal transcoding system. To do that we want to create a new “transcoded” version of our original file. Note: the original file will remain unchanged.

For this process, you’ll need to have FFmpeg installed on your machine.

Open the terminal and navigate to the directory that contains the audio or video file you want to transcode.

In one command, we can:

  • Convert the existing file to WAV format.

  • Adjust the sample rate to 16000 (16 kHz).

  • (If needed) convert a dual-channel file to a single-channel file.

The command takes two arguments, the name of the file you want to convert, and the name and extension you want to convert the file to, which in our case is .wav. For instance we can convert a file named test.mp3 with a command that looks like this:

ffmpeg -i input.mp3 -ar 16000 -ac 1 output.wav

Here’s what each flag in that command represents:

  • -i specifies that the following filename will be our input file.

  • -ar specifies that the following number will be our desired output sample rate.

  • -ac specifies that the following number will be our desired number of audio channels.

If changing the sample rate, channels, or format isn’t necessary, you can remove these flags as needed and/or leave the file extension the same.

Transcoding MP4 Files

There are occasions where transcoding MP4 files to WAV results in a different audio file length. This is because MP4 contains time stamp information unlike WAV, and this is needed to ensure the number of samples of the WAV file matches the target length. To address this, we need to apply a filter that ensures that the audio remains synchronized with the video stream or the timestamps.

ffmpeg -i input.mp4 -af aresample=async=1 -ac 1 -ar 16000 output.wav

Here’s what each flag in that command represents:

  • -af applies a specific filter in this case aresample=async=1 which allows the audio signal to be squeezed, stretched,etc to synchronize with the video stream.

  • -ar specifies that the following number will be our desired output sample rate.

  • -ac specifies that the following number will be our desired number of audio channels.

Transcoding Audio Files to FLAC

There are occassions where we will need to transcode a file to FLAC, specifically if requested by the Research team. To do so, you can use the following command:

ffmpeg -i input_file -ar 16000 -sample_fmt s16 -compression_level 5 output_file.flac

We also have a playbook How to Transcode Files to FLAC, which contains code to transcodes a folder of audio/video files to FLAC.