-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilibili_v3.py
54 lines (50 loc) · 1.86 KB
/
bilibili_v3.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
import argparse
import sys
import os
import json
def windows_file(file):
for i in '\\/:*?\"<>|':
file=file.replace(i,chr(ord(i)+65248))
return file
def run(path,add_number,**kw):
id_width=len(str(len(os.listdir(path))))
for root, dirs, files in os.walk(path):
if 'entry.json' in files:
with open(os.path.join(root,'entry.json'), encoding='utf8') as f:
entry=json.load(f)
path_main=windows_file(f"{entry['title']} [avid: {entry['avid']}]")
if not os.path.exists(path_main):
os.makedirs(path_main)
video_name=windows_file(entry['page_data']['part'])
video_id=entry['page_data']['page']
elif 'index.json' in files:
path_video=os.path.join(root,'video.m4s')
path_audio=os.path.join(root,'audio.m4s')
if add_number:
path_output=os.path.join(path_main,'{0:0{1}}.{2}.mp4'.format(video_id,id_width,video_name))
else:
path_output=os.path.join(path_main,f"{video_name}.mp4")
cmd=f'ffmpeg -v warning -y -i "{path_video}" -i "{path_audio}" -c copy "{path_output}"'
os.system(cmd)
def main():
parser=argparse.ArgumentParser()
parser.add_argument(
'-n',
help='Add number before file name.',
action='store_true',
dest='add_number'
)
parser.add_argument(
'-i',
help='Specify the input path. The default is to convert all folders in the current path',
dest='input_path'
)
args=parser.parse_args()
if args.input_path:
run(args.input_path,**args.__dict__)
else:
for dir in os.listdir('.'):
if os.path.isdir(dir):
run(dir,**args.__dict__)
if __name__ == "__main__":
main()