Skip to content

Instantly share code, notes, and snippets.

@feihong
Created February 4, 2015 21:41
Show Gist options
  • Save feihong/8dea9ae7c7055e8847d3 to your computer and use it in GitHub Desktop.
Save feihong/8dea9ae7c7055e8847d3 to your computer and use it in GitHub Desktop.
Script to add metadata to all .mp4 files within a directory
"""
Add metadata to all the .mp4 files within the given directory, based on the file
names of the input files.
Each input file should use the following format for their filename:
[album] [title] [artist]
Note that there are two spaces between each element.
Usage: python add_metadata.py /path/to/mp4/files/
In order to run this script, you must have ffmpeg installed. On Ubuntu, you can
do so by running `apt-get install ffmpeg`.
Sources:
http://superuser.com/questions/349518/how-to-use-ffmpeg-to-add-metadata-to-an-aac-file-without-reencoding
http://jonhall.info/how_to/create_id3_tags_using_ffmpeg
"""
import sys
import os
import os.path as op
import subprocess
if __name__ == '__main__':
dirname = sys.argv[1]
mp4_files = (f for f in os.listdir(dirname) if f.endswith('.mp4'))
for mp4_file in mp4_files:
input_path = op.join(dirname, mp4_file)
album, title, artist = op.splitext(mp4_file)[0].split(' ')
output_path = op.join(dirname, '%s - %s.mp4' % (artist, title))
cmd = [
'avconv',
'-i', input_path,
'-vn', '-acodec', 'copy', # copy, dont' reencode
'-metadata', 'title=' + title,
'-metadata', 'artist=' + artist,
'-metadata', 'album=' + album,
output_path,
]
print ' '.join(cmd)
subprocess.call(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment