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
| import getopt import json import os import re import shutil import sys
from ffmpy3 import FFmpeg
def get_sub_dir_names(file_dir): for root, dirs, files in os.walk(file_dir): return dirs
def get_sub_file_names(file_dir): for root, dirs, files in os.walk(file_dir): return files
def makedir(dir_name): if not os.path.exists(dir_name): os.makedirs(dir_name) print("---------------------------") print("文件夹创建成功!") print(dir_name) print("---------------------------")
def remove_punctuation(line): punctuation = ',。!?~!,;:?|/"\'' line = re.sub(r'[{}]+'.format(punctuation), '', line) punctuation = '【' line = re.sub(r'[{}]+'.format(punctuation), '[', line) punctuation = '】' line = re.sub(r'[{}]+'.format(punctuation), ']', line) punctuation = '<' line = re.sub(r'[{}]+'.format(punctuation), ']', line) punctuation = '>' line = re.sub(r'[{}]+'.format(punctuation), ']', line) return line.strip().lower()
def main(argv): input_dir = "" output_dir = "" try: opts, args = getopt.getopt(argv, "hi:o:", ["idir=", "odir="]) except getopt.GetoptError: print 'test.py -i <input_dir> -o <output_dir>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -i <input_dir> -o <output_dir>' sys.exit() elif opt in ("-i", "--idir"): input_dir = arg elif opt in ("-o", "--odir"): output_dir = arg
makedir(output_dir) for path in get_sub_dir_names(input_dir): file_path = os.path.join(input_dir, path) for sub_dir_name in get_sub_dir_names(file_path): sub_file_path = os.path.join(file_path, sub_dir_name) for info_file_name in get_sub_file_names(sub_file_path): info_file_path = os.path.join(sub_file_path, info_file_name) if info_file_path.endswith("entry.json"): info_file = open(info_file_path, encoding='UTF-8') info = info_file.read() title = json.loads(info)["title"] type_tag = json.loads(info)["type_tag"] download_subtitle = json.loads(info)["page_data"]["download_subtitle"] video_file_path = os.path.join(sub_file_path, type_tag) print("video_file_path", video_file_path) audio = os.path.join(video_file_path, "audio.m4s") video = os.path.join(video_file_path, "video.m4s") print("audio", audio) print("video", video) name = os.path.join(output_dir, path + " " + remove_punctuation(download_subtitle) + " " + sub_dir_name.zfill(2) + ".mp4") print(name) if os.path.exists(audio) and os.path.exists(video): ff = FFmpeg( inputs={video: None, audio: None}, outputs={name: '-c:v copy -c:a aac -strict experimental'} ) print(ff.cmd) ff.run() shutil.rmtree(file_path, ignore_errors=True)
if __name__ == "__main__": main(sys.argv[1:])
|