├── README.md └── jianying-srt.py /README.md: -------------------------------------------------------------------------------- 1 | ## 剪映字幕文件导出文本文件批量编辑 2 | 3 | 简介 4 | --- 5 | 6 | 剪映PC版有自动识别字幕功能,但是识别会有一定的误差,需要根据实际情况对字幕进行修改。剪映软件只能选中需要修改的单条字幕文本进行修改,字幕内容多的时候效率比较低。为了提高字幕编辑效率就开发了这个脚本把剪映的字幕内容导出到文本文件,集中修改好后再导回去。 7 | 8 | 参数说明 9 | --- 10 | python jianying-srt.py -j draft_content.json -t temp.txt -c totxt 11 | 12 | ### -j 13 | 选填,需要导出的剪映字幕文件完整路径,默认会在当前目录下查找draft_content.json文件。 14 | 15 | ### -t 16 | 选填,导出后的文本文件完整路径,默认会在当前目录下查找temp.txt文件。 17 | 18 | ### -c 19 | 选填,导入\导出选项,totxt是把剪映字幕json文件导出到文本文件,tojson是把编辑好的文本文件导入到剪映字幕json文件。 20 | 21 | 备注 22 | --- 23 | 剪映的字幕文件可以到它的安装目录找,以下是剪映默认的草稿路径: 24 | 25 | C:\Users\(你的用户名)\AppData\Local\JianyingPro\User Data\Projects\com.lveditor.draft 26 | 27 | 本脚本在剪映PC版V1.3.6下测试通过。 28 | 29 | -------------------------------------------------------------------------------- /jianying-srt.py: -------------------------------------------------------------------------------- 1 | # coding = utf-8 2 | 3 | import json 4 | import argparse 5 | 6 | 7 | def json_to_txt(json_file_name, txt_file_name): 8 | with open(json_file_name, 'r', encoding='utf-8') as f: 9 | jsons = json.load(f) 10 | 11 | # print(jsons['materials']['texts']) 12 | # print(jsons['tracks']) 13 | with open(txt_file_name, 'w', encoding='utf-8') as f: 14 | for item in jsons['materials']['texts']: 15 | f.write('%s,%s\n' % (item['id'], item['content'])) 16 | 17 | # for item in jsons['tracks']: 18 | # segments = item['segments'] 19 | # print(item['segments']) 20 | # for segment in segments: 21 | # print(segment['material_id']) 22 | # print(segment['target_timerange']) 23 | # print(segment['target_timerange']['duration']) 24 | # print(segment['target_timerange']['start']) 25 | 26 | 27 | def txt_to_json(txt_file_name, json_file_name): 28 | srt_dict = {} 29 | with open(json_file_name, 'r', encoding='utf-8') as f: 30 | jsons = json.load(f) 31 | 32 | with open(txt_file_name, 'r', encoding='utf-8') as f: 33 | for line in f.readlines(): 34 | a = line.strip('\n').split(',') 35 | srt_dict[a[0]] = a[1] 36 | 37 | for item in jsons['materials']['texts']: 38 | item['content'] = srt_dict[item['id']] 39 | print(item['content']) 40 | # print(item['id'], item['content']) 41 | 42 | with open(json_file, 'w', encoding='utf-8') as f: 43 | json.dump(jsons, f, ensure_ascii=False) # ensure_ascii=False 避免中文乱码 44 | 45 | 46 | if __name__ == '__main__': 47 | json_file = 'draft_content.json' 48 | txt_file = 'temp.txt' 49 | parser = argparse.ArgumentParser(description='剪映字幕导出批量修改工具。') 50 | parser.add_argument('-j', '--json_file_name', type=str, help='剪映字幕文件的完整路径,默认在当前目录下生成查找draft_content.json文件。') 51 | parser.add_argument('-t', '--txt_file_name', type=str, help='导出的字幕文本文件完整路径,默认在当前目录下生成temp.txt文件。') 52 | parser.add_argument('-c', '--command', type=str.lower, choices=['tojson', 'totxt'], 53 | help='转换类型,默认为把剪映字幕文件导出到文本文件(totxt)') 54 | args = parser.parse_args() 55 | if args.json_file_name: 56 | json_file = args.json_file_name 57 | if args.txt_file_name: 58 | txt_file = args.txt_file_name 59 | if args.command: 60 | command = args.command 61 | else: 62 | command = 'totxt' 63 | 64 | if command == 'totxt': 65 | json_to_txt(json_file_name=json_file, txt_file_name=txt_file) 66 | else: 67 | txt_to_json(txt_file_name=txt_file, json_file_name=json_file) 68 | --------------------------------------------------------------------------------