-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplevideodownload.py
56 lines (44 loc) · 1.48 KB
/
simplevideodownload.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import tempfile
import os
import sys
import shutil
import subprocess
import datetime
import json
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--uid', action='store', type=int, help='User id for the downloaded files')
parser.add_argument('--gid', action='store', type=int, help='Group id for the downloaded files')
parser.add_argument('url', action='store', nargs='*', help='Url to download')
args = parser.parse_args()
for url in args.url:
tempdir = tempfile.mkdtemp(dir=os.getcwd())
os.chdir(tempdir)
subprocess.call(["youtube-dl", url])
initial_path = os.listdir('.')[0]
subprocess.call(["rename.ul", "s/ /_/g", initial_path])
final_path = initial_path.replace(' ', '_')
base_path = final_path[:final_path.rfind('.')]
mp3_path = base_path + '.mp3'
subprocess.call(["ffmpeg", "-i", final_path, mp3_path])
date_str = datetime.datetime.today().strftime("%Y%m%d")
data = {
'date':date_str,
'url': url,
'final_path': final_path,
'mp3_path': mp3_path,
}
shutil.move(final_path, '../')
shutil.move(mp3_path, '../')
os.chdir('../')
json_file = base_path + '.json'
with open(json_file, 'w') as f:
f.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
for file_path in [json_file, final_path, mp3_path]:
os.chown(file_path, args.uid or -1, args.gid or -1)
shutil.rmtree(tempdir, ignore_errors=True)
if __name__ == '__main__':
main()