Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This page was copied adapted from a page in the LLILAS Benson Digital Inititaives wiki space, and much of what follows was put together by Ryan Sullivant, Digital Content Curator at AILLA.

...

The open-source FFmpeg engine can be used for several purposes. These include, converting between a/v containers and formats, re-encoding media with a different codec to produce smaller derivative access files, and rotating video. FFmpeg can be used as a command line tool or can be integrated into Python scripts using the ffmpeg-python package (github), which is useful for batch processes, especially if you are more comfortable using Python tools to identify which files need to be fed into FFmpeg (literal lists, reading from a file, regex filters with glob, etc.) than doing the same things with the command line, terminal, or powershell. As FFmpeg has no native MP3 encoder, another library, like LAME, may need to be installed to produce MP3 files.

Convert audio formats

Sometimes we receive digitized or born-digital audio in formats that are not supported by our Islandora repository. Circa 2019 at AILLA, these have included files in WMA, AIFF and OPUS (a lossly format used by WhatsApp). Staff should convert these files into the supported WAV or MP3 formats, depending on the nature and quality of the unsupported digital files. Format conversion with FFmpeg can be as simple as specifying a different extension for the output file, but additional details, such as those discussed in Creating smaller audio files, may also be specified.

Using the Windows command prompt:

ffmpeg -i input_file_path output_file_path

Using Python:

ffmpeg.input(input_file_path).output(output_file_path).run()

This syntax can also be used to extract MP3 audio from an MP4 file, by specifying the MP4 file as the input and the MP3 file as the output.

Batch converting audio formats

If you have a directory full of files that need to be converted from one format to another, you can perform this transformation on each of them in succession using a for loop in the command prompt:

for /f "tokens=1 delims=." %a in ('dir /b *.wma C:\directory\audio_files') do ffmpeg -i C:\directory\audio_files\%a.wma -acodec aac C:\directory\audio_files\%a.mp3

In the above example, all WMA files found within the C:\directory\audio_files folder will have MP3 copies created. These extensions can be changed to any format supported by FFmpeg to achieve different results

Creating smaller audio files

...

The two resolution options for audio (ar) are '48000' and '44100' (the former is more common with audio associated with video files, the second for standalone audio). Audio file bitrate (ab) can be specified as well. Stereo files can be converted to mono by specifying only one audio channel (ac). This should be done in cases there the original recording was made with one microphone, which is likely the case for many field recordings originally made on tape. Some digitization workflows will produce two audio channels even from mono recordings. In these cases, slight differences in the digitization settings and condition of the tape heads may result in slightly different left and right channels despite originally having the same input.

Python: 

ffmpeg.input(input_file_path).output(output_file_path, ab=16, ac=1, ar=48000).run()

CmdCommand prompt: 

ffmpeg -i input_file_path -ab 16 -ac 1 -ar 48000 output_file_path

Note that different commands may be used to alter the sizes of compressed files (like MP3).  

Convert audio formats

Sometimes we receive digitized or born-digital audio in formats that are not supported by our Islandora repository. Circa 2019 at AILLA, these have included files in WMA, AIFF and OPUS (a lossly format used by WhatsApp). Staff should convert these files into the supported WAV or MP3 formats, depending on the nature and quality of the unsupported digital files. Format conversion with FFmpeg can be as simple as specifying a different extension for the output file, but additional details, such as those discussed in Creating smaller audio files, may also be specified.

Cmd: ffmpeg -i input_file_path output_file_path

Python: ffmpeg.input(input_file_path).output(output_file_path).run()

This syntax can also be used to extract MP3 audio from an MP4 file, by specifying the MP4 file as the input and the MP3 file as the output.

Rotating video files

Some video files specify their orientation in their technical metadata. Sometimes, a file is incorrectly presented as portrait when it should be in landscape orientation and vice-versa. It is possible to transpose all pixels of the video, but a simple solution is to edit the file's technical metadata (while copying all other metadata) to inform video players of the proper orientation. This is done with the -map_metadata-metadata:s:v and -codec copy flags as below. Note that rotate specified the degrees of rotation in a counterclockwise orientation; '90' rotates counterclockwise and '270' rotates counterclockwise. It is unclear how this command (in particular the -metadata:s:v flag) can be implemented with ffmpeg-python, so this function can only be done via the command line currently.

CmdCommand prompt: 

ffmpeg -i input_file_path -map_metadata 0 -metadata:s:v rotate="270" -codec copy output_file_path

Creating smaller video files

Some high-definition cameras will use codecs, like XAVC S, that produce very large files, in part because each frame contains many many pixels. Many of these files may be too large to ingest directly into a repository. One way to produce a smaller file that is still suitable for online viewing is to re-encode the file with a different codec, one very widely supported codec to try is H.264. 

Python: 

ffmpeg.input(input_file_path).output(output_file_path,vcodec='h264').run()

Cmd: 

ffmpeg -i input_file_path -vcodec h264 output_file_path

If your video files have a high frame rate (that is, a multiple of 24, 25, or 30), then a lower frame rate can be specified with a command like -r 24 (cmd) or r='24' (python). 

...

Converting MP3s to AAC MP4s

To stream via the collections portal, audio must be The UT Libraries Collections Portal will only stream an audio file if it has been ingested into the DAMS as an MP4 file with AAC audio. While it's normally used for video files, the MP4 extension is really just a "wrapper" to combine audio and video streams, and there' s no requirement that there even be a video stream. That means you can convert from MP3 audio directly to MP4, and as long as you specify that the output will use AAC audio, it will work with the collections portalthe Collections Portal. Using the command prompt:

ffmpeg -i C:\directory\audio_file.mp3 -acodec aac C:\directory\output.mp4

...

for /f "tokens=1 delims=." %a in ('dir /b *.mp3 C:\directory\audio_file.mp3files') do ffmpeg -i C:\directory\audio_filefiles\%a.mp3 -acodec aac C:\directory\audio_files\%a.mp4

If no other options are specified, the AAC audio will match the sampling rate of the MP3 input audio.

...

In addition to converting and manipulating objects, FFmpeg can also be used to extract technical information about A/V files. Specific technical metadata fields can be filtered in the Linux command line using the grep command. The Using a Linux command line such as Ubuntu on the Windows Subsystem for Linux, the following script will produce a text list of filenames & runtimes:

...