/
album2mp4.py
171 lines (147 loc) · 5.59 KB
/
album2mp4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
from __future__ import print_function
from glob import glob
import argparse
import os
import subprocess
from PIL import Image
import platform
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
from mutagen.flac import FLAC
is_windows = platform.system() == 'Windows'
def which(program):
if is_windows:
program += '.exe'
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
executable = which('ffmpeg')
if executable is None:
executable = which('avconv')
if executable is None:
exit("Please install ffmpeg or avconv")
def create_cover(cover, folder):
"""
:return the cover art centered
"""
cover_im = Image.open(cover)
cover_im.thumbnail((1920, 1080), Image.ANTIALIAS)
out_fname = folder + 'cover_temp.png'
cover_out = Image.new('RGB', (1920, 1080))
img_w, img_h = cover_im.size
bg_w, bg_h = cover_out.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
cover_out.paste(cover_im, offset)
print("Generated cover:\t", out_fname)
cover_out.save(out_fname)
return out_fname
def do_process(folder, _cover, output, add_title, font, verbose, flac=False):
print("Folder:\t", folder)
print("Cover:\t", _cover)
print("Output:\t", output, "\n")
cover = create_cover(_cover, folder)
temps = []
descr = []
time = 0
glob_ext = ".flac" if flac else ".mp3"
for song in sorted(glob(folder + '*' + glob_ext)):
out_name = song.split(glob_ext)[0] + '.mp4'
if not flac:
title = EasyID3(song)['title'][0]
mp3 = MP3(song)
else:
mp3 = FLAC(song)
title = mp3['title'][0]
print(out_name)
temps.append(out_name)
descr.append("{0:0>2}:{1:0>2} - {2}".format(int(time) / 60, int(time) % 60, title))
args = [executable,
'-loop', '1',
'-y',
'-i', cover,
'-i', song
]
if add_title:
args += [
'-vf',
'drawtext=fontfile={}:text={}:fontcolor=white:fontsize=96:box=1:boxcolor=black@0.5:boxborderw=10:x=(w-tw)/2:y=(h/PHI)+th'.format(
font, "{}".format(title)
)]
args += [
'-c:v', 'libx264',
'-c:a', 'libmp3lame',
'-q:a', '2',
'-b:a', '320k',
'-shortest',
'-r', '5', out_name]
if verbose:
print(" ".join(args))
try:
convert_out = subprocess.check_output(args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(f"Failed {args} with ", exc.output.decode('utf8'))
exit()
if verbose:
print(convert_out)
time += mp3.info.length
filelisttxt = 'filelist.txt'
with open(filelisttxt, 'w') as file_list:
for line in temps:
file_list.write("file '{}'\n".format(line))
out = subprocess.check_output(
[executable, '-y', '-f', 'concat', '-safe', '0', '-i', filelisttxt, '-c', 'copy', output],
stderr=subprocess.STDOUT)
os.remove(filelisttxt)
os.remove(cover)
for t in temps:
os.remove(t)
if verbose:
print(out)
with open(output + '.txt', 'w') as descr_file:
descr_file.write("{}\n".format(os.path.split(folder)[-2]))
for line in descr:
descr_file.write(line + os.linesep)
descr_file.write("\nGenerated with https://github.com/jonnoftw/album2mp4")
if __name__ == "__main__":
# folder is $1
# cover art is $2
cwd = os.getcwd()
parser = argparse.ArgumentParser(
description='Turn a folder of mp3 files into a single video suitable for youtube upload')
parser.add_argument('-f', '--folder', metavar='folder', help='The folder containing the mp3 files', required=True,
default=cwd)
parser.add_argument('-x', '--flac', help='Search for flac files instead of mp3', action='store_true')
parser.add_argument('-o', '--output', metavar='output', help='The name of the output file',
default=os.path.split(os.getcwd())[-1] + '.mp4')
parser.add_argument('-c', '--cover', metavar='cover', help='Location of the cover art', default=cwd + '/cover.jpg')
parser.add_argument('-s', '--song-title', help='Add the song title to the video', action='store_true')
parser.add_argument('-p', '--font-path', metavar='font_path', help='Path to font file',
default='/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
)
parser.add_argument('-v', '--verbose', help='Provide verbose output', action='store_true')
args = parser.parse_args()
music_folder = args.folder
if music_folder[-1] != os.path.sep:
music_folder += os.path.sep
cover = args.cover
output = args.output
add_title = args.song_title
font = args.font_path
verbose = args.verbose
print("Converting folder of mp3 to mp4")
from datetime import datetime
startTime = datetime.now()
do_process(music_folder, cover, output, add_title, font.replace("\\", "/"), verbose, args.flac)
print("Finished in", datetime.now() - startTime)
print("Output in file:", output)