Skip to content

Instantly share code, notes, and snippets.

@persones
Created September 4, 2021 00:02
Show Gist options
  • Save persones/41c05b04e66c308b929580070940453a to your computer and use it in GitHub Desktop.
Save persones/41c05b04e66c308b929580070940453a to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 02 23:02:54 2017
@author: perso
"""
import os
import subprocess
import shutil
base_input = "f:\\albums\\"
#base_output = "f:\\albums_coverted"
base_output = "c:\\Users\\perso\\Documents\\albums_coversion"
ffmpeg = 'c:\\Progs\\ffmpeg-20161230-6993bb4-win64-static\\bin\\ffmpeg'
#ffmpeg = 'c:\\Progs\\ffmpeg-20161230-6993bb4-win64-static\\bin'
def convert_file(d, relpath):
v = '\\'
p = v.join(relpath) + '\\'
inputfile = base_input + p + d
if os.path.isdir(inputfile):
if inputfile == '.' or inputfile == '.':
#print "self or parent"
return
#print (d + " is a folder")
if not os.path.exists(base_output + p + d):
#print 'making dir ' + p + d
#os.makedirs(base_output + p + d)
pass
files = os.listdir(inputfile)
pp = list(relpath)
if d != "":
pp.append(d)
for f in files:
#print (f,pp)
convert_file (f, pp)
else:
nameList = os.path.splitext(d)
name = nameList[0]
if nameList[1] == '.ini':
print ".ini, aborting"
return
#print relpath
print (relpath, name)
if len(relpath) == 2:
artist = relpath[0]
album = relpath[1]
elif len(relpath) == 1:
sp = relpath[0].split(' - ')
artist = sp[0]
album = sp[1]
else:
print "can't parse album adn artist. try later...."
return
if album[-1] == ')' and album[-6] == '(':
album = album[:-7]
elif album[-1] == ')' and album[-2] == '(':
album = album[:-3]
#print relpath
print (artist, album)
artist_path = v.join([base_output, artist])
print artist_path
if not os.path.exists(artist_path):
print 'making dir ' + artist_path
os.makedirs(artist_path)
album_path = v.join([base_output, artist, album])
if not os.path.exists(album_path):
print 'making dir ' + album_path
os.makedirs(album_path)
print ("converting " + name , relpath)
output_name = v.join([album_path, name + '.mp3'])
print output_name
if nameList[1] == '.ini':
print ".ini, aborting"
return
if os.path.exists(output_name):
print "file exists, continuing"
return
if len(nameList) == 1 or (nameList[1] != '.wav' and nameList[1] != '.mp3'):
print "can't convert " + d + " it's a " + nameList[1] + ", copying..."
shutil.copyfile(inputfile, output_name)
else:
print subprocess.check_output([ffmpeg, '-i', inputfile, '-codec:a', 'libmp3lame', '-qscale:a', '320k', output_name], stderr=subprocess.STDOUT, shell=True)
#print subprocess.check_output(['dir', ffmpeg], stderr=subprocess.STDOUT, shell=True)
#os.system('c:\\Progs\\ffmpeg-20161230-6993bb4-win64-static\\bin\\ffmpeg -i "' + base_input + relpath + d + '" -code -codec:a libmp3lame -qscale:a 320k ' + base_output + relpath + name + '.mp3')
# ffmpeg -i "F:\Albums\Anouk - Urban Solitude (1999)\01 In The Sand.wav -codec:a libmp3lame -qscale:a 2 output.mp3
convert_file("", [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment