Skip to content

Instantly share code, notes, and snippets.

@gelin
Created February 20, 2020 15:32
Show Gist options
  • Save gelin/883b3ecce9652c6246bed3cf0c066037 to your computer and use it in GitHub Desktop.
Save gelin/883b3ecce9652c6246bed3cf0c066037 to your computer and use it in GitHub Desktop.
Copy/convert audio files to car audio: convert file to mp3, apply mp3gain, convert tags to ascii
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
import os.path
import shutil
import subprocess
import glob
import traceback
from codecs import decode, encode
from transliterate.utils import translit
from mutagen.easyid3 import EasyID3
def copy(path, target):
path = os.path.abspath(path)
if os.path.isdir(path):
copy_dir(path, target)
elif os.path.isfile(path):
copy_file(path, target)
def is_mp3(name):
(root, ext) = os.path.splitext(name)
return ext == '.mp3'
def copy_dir(path, target):
target_dir = os.path.join(target, to_ascii(os.path.basename(path)))
if not os.path.isdir(target_dir):
print('creating', target_dir, '...')
os.makedirs(target_dir)
for root, dirs, files in os.walk(path):
for name in sorted(dirs):
copy_dir(os.path.join(root, name), target_dir)
for name in sorted(files):
copy_file(os.path.join(root, name), target_dir)
def copy_file(path, target):
target_file = os.path.join(target, to_ascii(os.path.basename(path)))
if not is_mp3(path):
(root, ext) = os.path.splitext(target_file)
target_file = root + '.mp3'
if os.path.isfile(target_file):
print(target_file, 'already exists')
return
if is_mp3(path):
print('coping', path, 'to', target_file, '...')
shutil.copy(path, target_file)
else:
convert_to_mp3(path, target_file)
def convert_to_mp3(path, target):
print('converting', path, 'to', target, '...')
#TODO: check ffmpeg availability
os.spawnlp(os.P_WAIT, 'avconv', 'avconv',
'-i', path, '-vn', '-acodec', 'libmp3lame', '-ab', '128k', target)
def traverse_target(target):
fix_gain(target)
for root, dirs, files in os.walk(target):
for name in dirs:
fix_gain(os.path.join(root, name))
for name in files:
convert_tags(os.path.join(root, name))
def is_ascii(string):
return not string or ord(max(string)) < 128
def to_ascii(name):
if is_ascii(name):
return name
return translit(unicode(name, 'utf8'), 'ru', reversed=True)
def fix_gain(dir_name):
mp3_files = os.path.join(dir_name, '*.mp3')
print('fixing gain for', mp3_files)
subprocess.call(['mp3gain', '-a'] + glob.glob(mp3_files))
def convert_tags(file_name):
print('converting tags for', file_name)
def ascii_tag(name):
values = audio[name]
for (i, value) in enumerate(values):
#print(value)
value = encode(value, 'utf8')
if not is_ascii(value):
ascii_value = to_ascii(value)
print(value, '->', ascii_value)
values[i] = ascii_value
audio[name] = values
try:
audio = EasyID3(file_name)
#print(audio.valid_keys.keys())
ascii_tag('title')
ascii_tag('artist')
ascii_tag('album')
audio.save()
except:
traceback.print_exception(sys.exc_type, sys.exc_value, None)
if __name__=='__main__':
if len(sys.argv) < 2:
print('Usage: cp4prology [source_file_or_dir ...] target_dir')
sys.exit(1)
target = sys.argv[-1]
for path in sys.argv[1:-1]:
copy(path, target)
traverse_target(target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment