-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathputio_mp4conv.py
More file actions
100 lines (67 loc) · 2.53 KB
/
putio_mp4conv.py
File metadata and controls
100 lines (67 loc) · 2.53 KB
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
#!/usr/bin/python
import putio
from subprocess import call
import os
dl_dirname = 'youtube'
ul_dirname = 'ytmp3'
done_dirname = 'ytconverted'
def get_file_id(name):
searchresult = putio.search(confdict, name)
result = [f['id'] for f in searchresult['files'] if f['parent_id'] == 0]
return result[0]
def get_file_list():
dlid = get_file_id(dl_dirname)
dlfilelist = putio.get_filelist(confdict,dlid)
dlfileids = [fid["id"] for fid in dlfilelist["files"]]
return dlfileids
def convert_and_upload_every_file(fileList=[]):
ulid = get_file_id(ul_dirname)
for id in fileList:
filename = download_mp4(id)
print("file: "+filename)
mp3_filepath = convert_to_mp3(filename)
upload_mp3(mp3_filepath,ulid)
cleanup(id, mp3_filepath)
def download_mp4(id):
return putio.download_file(confdict, id, "./working")
def convert_to_mp3(filename_with_extension):
indir = "./working"
outdir = "./outdir"
if filename_with_extension[-4:] == ".mp4":
return convert_mp4_to_mp3(filename_with_extension, indir, outdir)
elif filename_with_extension[-5:] == ".webm":
return convert_webm_to_mp3(filename_with_extension,indir,outdir)
def convert(convertfunc):
def convert_wrapper(filename_with_extension ,indir, outdir):
filename = ".".join(filename_with_extension.split(".")[0:-1])
if not os.path.exists(outdir):
os.makedirs(outdir)
convertfunc(filename, indir, outdir)
os.remove(indir + "/" + filename_with_extension)
return outdir + "/" + filename + ".mp3"
return convert_wrapper
@convert
def convert_mp4_to_mp3(filename, indir, outdir):
call(["ffmpeg", "-i", indir + "/" + filename + ".mp4", "-b:a", "192K",
"-vn", outdir + "/" + filename + ".mp3"])
@convert
def convert_webm_to_mp3(filename, indir, outdir):
call(["ffmpeg", "-i", indir + "/" + filename + ".webm", "-vn", "-ab",
"128k", "-ar", "44100", "-y", outdir + "/" + filename + ".mp3"])
def upload_mp3(mp3_path, upload_id):
putio.upload_file(confdict, mp3_path, upload_id)
def cleanup(id, mp3_path):
move_mp4_in_putio(id)
delete_mp3_on_host(mp3_path)
def move_mp4_in_putio(id):
done_id = get_file_id(done_dirname)
putio.move_file(confdict, id, done_id)
def delete_mp3_on_host(mp3_path):
os.remove(mp3_path)
def main():
global confdict
confdict = putio.config()
fileList = get_file_list()
convert_and_upload_every_file(fileList)
if __name__ == '__main__':
main()