Skip to content

Instantly share code, notes, and snippets.

@sjmielke
Created August 31, 2015 07:44
Show Gist options
  • Save sjmielke/cbd043788f82eeb73029 to your computer and use it in GitHub Desktop.
Save sjmielke/cbd043788f82eeb73029 to your computer and use it in GitHub Desktop.
opusify
#! /usr/bin/env python3
import os
import sys
import shutil
import subprocess
import mutagen.flac
import mutagen.mp3
import mutagen.mp4
import mutagen.oggopus
import mutagen.oggvorbis
import mutagen.asf
# Takes a directory to walk.
oldroot = sys.argv[1]
newroot = sys.argv[2]
musicexts = ["flac", "wav", "mp3", "m4a", "opus", "ogg", "wma"]
imageexts = ["png", "PNG", "jpg", "JPG", "jpeg", "JPEG"]
otherexts = ["cue", "log", "m3u", "m3u8", "doc", "rtf", "nfo", "sfv", "sfk", "db", "ini", "pdf", "txt", "mid", "md5", "ffp", "accurip", "url", "ico", "htm", "html", "ds_store", "zip", "rar", "exe", "avi", "mp4", "tif", "bmp"]
def get_ext(s):
return s.split('.')[-1].lower()
def copy_cover_imagery(innerpath, subfiles):
for filename in subfiles:
for pref in ["cover", "front", "folder"]:
for ext in imageexts:
if filename == "{}.{}".format(pref, ext):
innerfilename = innerpath + os.sep + filename
shutil.copy2(oldroot + innerfilename, cleanchars(newroot + innerfilename))
return
def get_bitrate(filename):
if get_ext(filename) in ["flac", "wav"]:
return 999999 # Convert anyway
elif get_ext(filename) in ["opus"]:
return 0 # Don't convert
elif get_ext(filename) == "mp3":
return mutagen.mp3.MP3(filename).info.bitrate
elif get_ext(filename) == "m4a":
return mutagen.mp4.MP4(filename).info.bitrate
elif get_ext(filename) == "ogg":
return mutagen.oggvorbis.OggVorbis(filename).info.bitrate
elif get_ext(filename) == "wma":
print(filename)
return mutagen.asf.ASF(filename).info.bitrate
return 0
def cleanchars(s):
return s.replace("*", "_").replace("?", "_").replace(":", "_")
def trans_file(innerfilename):
def log(s):
with open("/tmp/opusify_log", "a") as f: f.write(s)
kbr = get_bitrate(oldroot + innerfilename) // 1000
if kbr > 160:
ffmpeg = subprocess.Popen(["ffmpeg", "-i", oldroot + innerfilename, "-f", "flac", "-"],
stdout=subprocess.PIPE)
opusenc = subprocess.call(["opusenc", "--bitrate", "128", "-", newroot + cleanchars(innerfilename) + ".opus"],
stdin=ffmpeg.stdout)
ffmpeg.wait()
if ffmpeg.returncode != 0: log("FFmpeg: " + oldroot + innerfilename + "\n")
if opusenc != 0: log("opusenc: " + oldroot + innerfilename + "\n")
else:
shutil.copy2(oldroot + innerfilename, newroot + cleanchars(innerfilename))
def copy_music(innerpath, subfiles):
for filename in subfiles:
if get_ext(filename) in musicexts:
trans_file(innerpath + os.sep + filename)
elif get_ext(filename) in imageexts + otherexts:
pass
else:
print("Unsure how to deal with: " + innerpath + filename)
# Main
for (curpath, subdirs, subfiles) in os.walk(sys.argv[1]):
# Skip hidden directories
ishidden = False
for somedir in curpath.split(os.sep):
if len(somedir) > 1 and somedir[0] == '.':
ishidden = True
if ishidden: continue
innerpath = curpath[len(oldroot):]
# Skip already converted things (=hacked resumability)
if os.path.exists(newroot + cleanchars(innerpath)): # doesnt check if its a directory but who cares
continue
os.makedirs(newroot + cleanchars(innerpath), exist_ok=True)
copy_cover_imagery(innerpath, subfiles)
copy_music(innerpath, subfiles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment