-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenameSubtitles_RE_v1.0.6.bat
1454 lines (1347 loc) · 37.6 KB
/
RenameSubtitles_RE_v1.0.6.bat
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@echo off
rem chcp 65001
:DEBUG
if not defined debug (
echo [M_DE] 正在启动DEBUG层
set "debug=1"
cmd /c call "%~0"
set "debug="
title %ComSpec%
echo [M_DE] 运行结束
pause
goto :EOF
)
rem -------------------------------------------------------
rem AUTO SEARCH MODE SETTINGS 自动模式参数设置
rem -------------------------------------------------------
rem [count_range]
rem 控制compare_list进行比较的项数,可以加快AUTO,但错误率增大
set "count_range=15"
rem 可用值
rem {1~n(非负整数)}(从0开始计数,即=15时会比较16项)
rem {留空}(不启用,即全项目检索)
rem 推荐值
rem {10~20}(不建议少于10)
rem {≥100}(对于集数超过100的)
rem -------------------------------------------------------
rem [ls_step]
rem 控制long_string每一循环计数的字符量,值越大对长字符串的计数就越快,但对短字符串就越慢
set "ls_step=5"
rem 可用值
rem {1~n(非负整数)}
rem 推荐值
rem {5~10}
rem -------------------------------------------------------
rem [com_step]
rem 控制compare_string每一循环计数的字符量,值越大对长字符串的计数就越快,但对短字符串就越慢
set "com_step=5"
rem 可用值
rem {1~n(非负整数)}
rem 推荐值
rem {5~10}
rem -------------------------------------------------------
rem [ext_filters]
rem [subext]:字幕扩展名过滤
rem [vidext]:视频扩展过滤
rem 控制AUTO模式扩展名过滤器可输入的扩展名
rem 注意:必须在整个变量的尾部也加上一个";",否则无法读取最后一个扩展名
set "subext=ass;ssa;srt;idx;pgs;sup;smi;"
set "vidext=mp4;m4v;mkv;m2ts;avi;mpeg;mpg;"
rem -------------------------------------------------------
rem [use_same_long_filename]
rem [same_long_v]:用于视频搜索
rem [same_long_s]:用于字幕搜索
rem
rem 该选项控制是否启用旧的 同长度文件名筛选 算法
rem 在该旧算法下,只有同样长度的文件会进行对比
rem 适用于规范的、格式化的、有一定规律的文件名
set "same_long_v="
set "same_long_s="
rem 可用值
rem {未定义}:不使用该算法
rem {任意值}:使用旧算法
rem 注
rem {未定义}即直接 set "same_long_v=" 不需要空格
rem -------------------------------------------------------
rem [enable_ext_use_most]
rem [ext_use_most_v]:用于视频搜索
rem [ext_use_most_s]:用于字幕搜索
rem 该选项控制是否启用旧的 扩展名筛选 算法
rem 在该算法下,只有文件夹下文件数量最多的扩展名才会被使用
rem 适用于普遍使用同一扩展名的文件
set "ext_use_most_v="
set "ext_use_most_s="
rem 可用值
rem {未定义}:不使用该算法
rem {任意值}:使用旧算法
rem 注
rem {未定义}即直接 set "ext_use_most_v=" 不需要空格
rem -------------------------------------------------------
set "prefile=%~dp0rename_preview.log"
set "logfile=%~dp0rename_log.log"
set "vpfile=%~dp0vp_cache.log"
set "undofile=%~dp0undo_rename.log"
set "vffile=%~dp0vf_rename.log"
set "sffile=%~dp0sf_rename.log"
if exist "%undofile%" del /q "%undofile%"
if exist "%prefile%" del /q "%prefile%"
if exist "%logfile%" del /q "%logfile%"
if exist "%vpfile%" del /q "%vpfile%"
if exist "%vffile%" del /q "%vffile%"
if exist "%sffile%" del /q "%sffile%"
set "bat_ver=RenameSubtitles RE: Ver1.0.6 GB2312"
title %bat_ver% [(c) yyfll]
echo [INFO] %bat_ver%
echo [INFO] Copyright(c) 2019-2020 yyfll
:MAIN
echo.
echo [ 视频目录 ]
set /p v_path=[V_IN] V_PATH:
rem goto v_check_end
echo [CHKV] 正在检查视频目录CMD兼容性及可用性
call :check_path_cmd
if defined path_inv (
set "path_inv="
goto MAIN
)
if not exist "%v_path%" (
echo [V_EX] 找不到视频路径
goto MAIN
)
echo [CHKV] 视频目录确认成功
:MAIN_S
echo.
echo [ 字幕目录(留空默认同视频目录) ]
set /p sub_path=[S_IN] S_PATH:
if not defined sub_path (
echo [VOID] 自动套用视频目录
set "sub_path=%v_path%"
goto s_check_end
)
echo [CHEK] 正在检查字幕目录CMD兼容性及可用性
echo.
rem goto s_check_end
call :check_path_cmd_s
if defined path_inv (
set "path_inv="
goto MAIN_S
)
if not exist "%sub_path%" (
echo [S_EX] 找不到字幕路径
goto MAIN_S
)
echo [CHKS] 字幕目录确认成功
if not "%sub_path:~0,-1%"=="\" set "sub_path=%sub_path%\"
:s_check_end
echo.
echo [ 自动生成匹配规则 即通过分析文件名寻找匹配规则 ]
echo [ 由于批量处理程序比较简单 分析出错率比较大 ]
echo [ 而且自动 非常慢! 非常慢! 非常慢! 非常慢! ]
echo [ 不是很懒的人非常非常非常建议手动输入匹配规则 ]
echo [ 预计时长 约6分钟完成13话分析 于 酷睿2DuoP8400 ]
echo [ 自动匹配 ([y]尝试/[n]不尝试) ]
set /p auto=[AUTO] Auto[y/n]?:
if /i "%auto%"=="y" (
goto AUTO_POINT
) else if /i "%auto%"=="n" (
set "auto="
) else (
set "auto="
)
echo.
echo [ 匹配规则即如何匹配视频 请用半角问号代替集数 ]
echo [ 例如[CASO][El_Cazador][GB_BIG5][01][DVDRIP] ]
echo [ 替换成[CASO][El_Cazador][GB_BIG5][??][DVDRIP] ]
echo [ 如果视频有非常麻烦的如校验值在文件名结尾 ]
echo [ 请使用通配符 如[CASO][*][GB_BIG5][??][* ]
echo [ 视频匹配规则 ]
set /p filter=[V_FI] V_FILTER:
echo.
echo [ 字幕匹配规则 ]
set /p s_filter=[S_FI] S_FILTER:
echo.
:AUTO_POINT
if defined auto (
cls
echo 在自动搜索之前,我们需要您填写一些必要信息
echo.
)
echo [ 剧集范围即是从多少集到多少集 请以 1,24 表示 ]
echo [ 如我的片有52话 从第01话开始 那么我就写1,52 ]
echo [ 注意必须要用半角逗号 , 否则不被识别 ]
:re_get_ep
echo [ 剧集范围 ]
set /p ep=[EP_R] EP_Range:
for /f "tokens=1,2 delims=," %%a in ("%ep%") do (
if "%%~a"=="" (
echo [ERRE] 剧集输入有误
goto re_get_ep
)
if "%%~b"=="" (
echo [ERRE] 剧集输入有误
goto re_get_ep
)
)
echo.
echo [ 字幕注释信息即文件名后跟的如 .CASO-sc 这样的 ]
echo [ 这种用于表示字幕组与字幕语言的文本 就是注释 ]
echo [ 如[YYDM-11FANS][Rozen Maiden][01].CASO-sc.ass ]
echo [ 字幕注释(可以留空) ]
set /p s_rem=[SANN] S_Annotate:
if not defined s_rem goto norem_sub
if not "%s_rem%"=="" if not "%s_rem:~0,1%"=="." set "s_rem=.%s_rem%"
:norem_sub
if defined auto goto AUTO
echo.
echo [ 搜索子目录即 搜索指定字幕目录之下的所有目录 ]
echo [ 如果您的字幕文件夹底下还有其他类似字幕 不推荐 ]
echo [ 一般匹配规则可以替代该选项 请自行决定是否启用 ]
echo [ 子目录搜索([y]启用^/[n]停用) ]
set /p ass_subdir=[SDIR] S_Dir[y/n]:
if "%ass_subdir%"=="1" (
set "ass_subdir=y"
) else if "%ass_subdir%"=="2" (
set "ass_subdir=n"
) else (
set "ass_subdir=n"
)
:AUTO_BACK
cls
echo [WORK] 正在匹配规则中寻找剧集位置...
:GET_COUNT
set "get_loop=0"
set "instr[0]="
set "instr[1]="
set "get_count=0"
set "stop_count=0"
set "working_filter=0%filter%"
:FIND_COUNT
set "working_filter=%working_filter:~1%"
if not defined working_filter goto END_FIND_COUNT
set "a_char=%working_filter:~0,1%"
if "%a_char%"=="?" (
set /a get_count=get_count+1
if not defined instr[0] (
set "instr[0]=%get_loop%"
) else set /a stop_count=stop_count+1
)
if %stop_count% GTR 0 goto END_FIND_COUNT
if not "%a_char%"=="" (
set /a get_loop=get_loop+1
goto FIND_COUNT
) else goto END_FIND_COUNT
goto FIND_COUNT
:END_FIND_COUNT
set "instr[1]=%get_count%"
echo [VINS] 在视频匹配规则中找到位置%instr[0]%,长度%instr[1]%
if not defined s_filter goto END_FIND_SUB_COUNT
set "get_loop=0"
set "s_instr[0]="
set "s_instr[1]="
set "get_count=0"
set "stop_count=0"
set "working_filter=0%s_filter%"
:FIND_SUB_COUNT
set "working_filter=%working_filter:~1%"
if not defined working_filter goto END_FIND_SUB_COUNT
set "a_char=%working_filter:~0,1%"
if "%a_char%"=="?" (
set /a get_count=get_count+1
if not defined s_instr[0] (
set "s_instr[0]=%get_loop%"
) else set /a stop_count=stop_count+1
)
if %stop_count% GTR 0 goto END_FIND_SUB_COUNT
if not "%a_char%"=="" (
set /a get_loop=get_loop+1
goto FIND_SUB_COUNT
) else goto END_FIND_SUB_COUNT
goto FIND_SUB_COUNT
:END_FIND_SUB_COUNT
set "working_filter="
set "s_instr[1]=%get_count%"
echo [SINS] 在字幕匹配规则中找到位置%s_instr[0]%,长度%s_instr[1]%
echo.
:LOOP_NUM
title %bat_ver% ^| 视频字幕匹配中...
for /f "tokens=1,2 delims=," %%a in ("%ep%") do (
for /l %%c in (%%~a,1,%%~b) do (
echo [LOOP] 尝试匹配剧集%%~c
call :SEARCH_VIDEO "%%~c"
if not defined not_work (
call :SEARCH_ASS "%sub_path%" "%s_filter%" "%%~c" "%ass_subdir%"
) else set "not_work="
)
)
if not exist "%prefile%" (
echo [ERR_] 找不到匹配的重命名项
goto end_batch
)
if not exist "%logfile%" (
echo [ERR_] 找不到匹配的重命名项
goto end_batch
)
:preview_check
cls
title %bat_ver% ^| 重命名预览
echo [PREV] 重命名预览
echo.
type "%prefile%"
echo [PREV] 预览显示完毕
choice /M "[CHK_] 要执行这样的重命名吗?"
if %errorlevel%==0 goto preview_check
if %errorlevel%==1 (
echo.
for /f "usebackq tokens=1,2 delims=^|" %%a in ("%logfile%") do (
call :rename_sub "%%~a" "%%~b"
)
if exist "%prefile%" if not "%sub_path:~-1%"=="\" (
move /y "%prefile%" "%sub_path%\rename_log.log"
if exist "%undofile%" move /y "%undofile%" "%sub_path%\undo_rename.log"
) else (
move /y "%prefile%" "%sub_path%rename_log.log"
if exist "%undofile%" move /y "%undofile%" "%sub_path%undo_rename.log"
)
)
:end_batch
if exist "%undofile%" del /q "%undofile%"
if exist "%logfile%" del /q "%logfile%"
if exist "%prefile%" del /q "%prefile%"
if exist "%vpfile%" del /q "%vpfile%"
if exist "%vffile%" del /q "%vffile%"
if exist "%sffile%" del /q "%sffile%"
setlocal DisableDelayedExpansion
goto :EOF
:SEARCH_VIDEO
if not "%v_path:~-1%"=="\" (
set "v_path_1=%v_path%\"
) else set "v_path_1=%v_path%"
set "vid_num=%~1"
set "zero_loop=0"
if "%instr[1]%"=="1" goto :end_add_zero
:add_zero
setlocal EnableDelayedExpansion
set "vid_num_r=%vid_num%"
set "vid_num=00000000000000000000000%vid_num%"
set "vid_num=!vid_num:~-%instr[1]%!"
:end_add_zero
set "not_work="
set "vid_path[0]=!filter:~0,%instr[0]%!"
for /f "tokens=*" %%a in ('set /a instr[0]+instr[1]') do (
set "vid_path[1]=!filter:~%%~a!"
)
setlocal DisableDelayedExpansion
set "vid_path=%vid_path[0]%%vid_num%%vid_path[1]%"
set "vid_path_1=%vid_path[0]%%vid_num_r%%vid_path[1]%"
for /r "%v_path%" %%a in ("%vid_path%") do (
if exist "%%~a" (
set "video=%%~a"
echo [VGET] 匹配到视频"%%~nxa"
goto got_video
)
)
for /r "%v_path%" %%a in ("%vid_path_1%") do (
if exist "%%~a" (
set "video=%%~a"
echo [VGET] 匹配到视频"%%~nxa"
goto got_video
)
)
echo [ERR_] 未能匹配到视频
set "not_work=0"
:got_video
echo "%video%">"%vpfile%"
goto :EOF
rem call :SEARCH_ASS "[sub_path]" "[filter]" "[ep_num]" "[sub-dir][y/n] [unset]"
:SEARCH_ASS
set "ass_path=%~1"
if not "%~2"=="" (
rem for /f "tokens=* delims=^|" %%a in ("%s_filter%") do (
rem call :CHECK_EXT "%%~xa" "n"
rem )
set "s_filter=%~2"
) else set "s_filter=*.ass"
if /i "%~4"=="y" (
set "ass_subdir=0"
) else set "ass_subdir="
if not exist "%~1" (
echo [ERROR] Subtitles Path No Found
pause
goto :EOF
)
set "sub_num=%~3"
set "zero_loop=0"
if "%s_instr[1]%"=="1" goto :end_s_add_zero
:s_add_zero
setlocal EnableDelayedExpansion
set "sub_num_r=%sub_num%"
set "sub_num=00000000000000000000000%sub_num%"
set "sub_num=!sub_num:~-%s_instr[1]%!"
:end_s_add_zero
set "s_path[0]=!s_filter:~0,%s_instr[0]%!"
for /f "tokens=*" %%a in ('set /a s_instr[0]+s_instr[1]') do (
set "s_path[1]=!s_filter:~%%~a!"
)
setlocal DisableDelayedExpansion
set "s_path=%s_path[0]%%sub_num%%s_path[1]%"
set "s_path_1=%s_path[0]%%sub_num_r%%s_path[1]%"
for /r "%ass_path%" %%a in ("%s_path%") do (
if exist "%%~a" (
if "%%~xa"==".idx" (
if exist "%%~dpna.sub" (
echo [SGET] 匹配到字幕"%%~na" [IDX+SUB]
) else (
echo [ERRS] 找不到与IDX文件相匹配的SUB文件
goto got_sub
)
) else if "%%~xa"==".sub" (
if exist "%%~dpna." (
echo [SGET] 匹配到字幕"%%~na" [IDX+SUB]
) else (
echo [ERRS] 找不到与IDX文件相匹配的SUB文件
goto got_sub
)
) else echo [SGET] 匹配到字幕"%%~nxa"
if defined ass_subdir (
if "%%~dpa"=="%ass_path%" (
call :preview_sub "%%~a"
) else (
echo [ERR_] 匹配字幕不在目录下
)
) else (
call :preview_sub "%%~a"
)
goto got_sub
)
)
for /r "%ass_path%" %%a in ("%s_path_1%") do (
if exist "%%~a" (
if "%%~xa"==".idx" (
if exist "%%~dpna.sub" (
echo [SGET] 匹配到字幕"%%~na" [IDX+SUB]
) else (
echo [ERRS] 找不到与IDX文件相匹配的SUB文件
goto got_sub
)
) else if "%%~xa"==".sub" (
if exist "%%~dpna." (
echo [SGET] 匹配到字幕"%%~na" [IDX+SUB]
) else (
echo [ERRS] 找不到与IDX文件相匹配的SUB文件
goto got_sub
)
) else echo [SGET] 匹配到字幕"%%~nxa"
if defined ass_subdir (
if "%%~dpa"=="%ass_path%" (
call :preview_sub "%%~a"
) else (
echo [ERR_] 匹配字幕不在目录下
)
) else (
call :preview_sub "%%~a"
)
goto got_sub
)
)
echo [ERR_] 未能匹配到字幕
:got_sub
set "s_path[0]="
set "s_path[1]="
setlocal EnableDelayedExpansion
goto :EOF
:rename_sub
rename "%~1" "%~nx2"
if not exist "%~2" (
echo [ERR_] 未能重命名字幕"%~nx1"
) else (
echo [REN_] 成功重命名字幕"%~nx2"
echo "%~2"^|"%~nx1">>"%undofile%"
)
goto :EOF
:preview_sub
for /f "tokens=* usebackq" %%a in ("%vpfile%") do (
set "v_name=%%~na"
)
if "%v_name%"=="" goto :EOF
if not "%~x1"==".idx" if not "%~x1"==".sub" (
(echo "%~nx1"
echo --^> "%v_name%%s_rem%%~x1"
echo.)>>"%prefile%"
if not "%ass_path:~-1%"=="\" (
echo "%~1"^|"%ass_path%\%v_name%%s_rem%%~x1">>"%logfile%"
) else echo "%~1"^|"%ass_path%%v_name%%s_rem%%~x1">>"%logfile%"
)
if "%~x1"==".sub" (
(echo "%~nx1" ^& "%~n1.idx"
echo --^> "%v_name%%s_rem%%~x1"
echo --^> "%v_name%%s_rem%.idx"
echo.)>>"%prefile%"
if not "%ass_path:~-1%"=="\" (
echo "%~1"^|"%ass_path%\%v_name%%s_rem%%~x1">>"%logfile%"
echo "%~dpn1.idx"^|"%ass_path%\%v_name%%s_rem%.idx">>"%logfile%"
) else (
echo "%~1"^|"%ass_path%%v_name%%s_rem%%~x1">>"%logfile%"
echo "%~dpn1.idx"^|"%ass_path%%v_name%%s_rem%.idx">>"%logfile%"
)
) else if "%~x1"==".idx" (
(echo "%~nx1" ^& "%~n1.sub"
echo --^> "%v_name%%s_rem%%~x1"
echo --^> "%v_name%%s_rem%.sub"
echo.)>>"%prefile%"
if not "%ass_path:~-1%"=="\" (
echo "%~1"^|"%ass_path%\%v_name%%s_rem%%~x1">>"%logfile%"
echo "%~dpn1.sub"^|"%ass_path%\%v_name%%s_rem%.sub">>"%logfile%"
) else (
echo "%~1"^|"%ass_path%%v_name%%s_rem%%~x1">>"%logfile%"
echo "%~dpn1.sub"^|"%ass_path%%v_name%%s_rem%.sub">>"%logfile%"
)
)
goto :EOF
:Module_AUTO-SEARCH
:AUTO
echo [AUTO] 自动搜索模块 VER1.0 Alpha
setlocal EnableDelayedExpansion
:ToLIST
echo [LIST] 正在转换扩展名表
set "ass_loop=0"
set "ass_count=0"
set "ass_start=0"
:GET_EXT_LOOP_ASS
set "a_char=!subext:~%ass_loop%,1!"
if "%a_char%"=="" goto :END_GET_EXT_LOOP_ASS
if not "%a_char%"==";" (
set /a ass_loop=ass_loop+1
goto GET_EXT_LOOP_ASS
)
for /f "tokens=*" %%a in ('set /a ass_loop-ass_start') do (
set "subext[%ass_count%]=.!subext:~%ass_start%,%%~a!"
)
set /a ass_count=ass_count+1
set /a ass_loop=ass_loop+1
set "ass_start=%ass_loop%"
goto :GET_EXT_LOOP_ASS
:END_GET_EXT_LOOP_ASS
set "ass_loop=0"
set "ass_count=0"
set "ass_start=0"
:GET_EXT_LOOP_VIDEO
set "a_char=!vidext:~%ass_loop%,1!"
if "%a_char%"=="" goto :END_GET_EXT_LOOP_VIDEO
if not "%a_char%"==";" (
set /a ass_loop=ass_loop+1
goto GET_EXT_LOOP_VIDEO
)
for /f "tokens=*" %%a in ('set /a ass_loop-ass_start') do (
set "vidext[%ass_count%]=.!vidext:~%ass_start%,%%~a!"
)
set /a ass_count=ass_count+1
set /a ass_loop=ass_loop+1
set "ass_start=%ass_loop%"
goto :GET_EXT_LOOP_VIDEO
:END_GET_EXT_LOOP_VIDEO
echo [LIST] Complete making EXT lists.
set "rf=%~dp0auto_rename\"
if not exist "%rf%" mkdir "%rf%" 1>nul 2>nul
:AUTO_MAIN
del /q "%rf%*.log" 2>nul
cls
title %bat_ver% ^| 自动匹配视频规则中...
call :try_video
cls
title %bat_ver% ^| 自动匹配字幕规则中...
del /q "%rf%*.log" 2>nul
call :try_subtitles
cls
del /q "%rf%*.log" 2>nul
rmdir "%rf%" 2>nul
setlocal DisableDelayedExpansion
if exist "%vffile%" (
for /f "tokens=* usebackq" %%a in ("%vffile%") do set "filter=%%~a"
) else (
echo [ERRA] 找不到输出的匹配规则文件
pause
goto end_batch
)
if exist "%sffile%" (
for /f "tokens=* usebackq" %%a in ("%sffile%") do set "s_filter=%%~a"
) else (
echo [ERRA] 找不到输出的匹配规则文件
pause
goto end_batch
)
echo [AUTO] 自动视频匹配规则: "%filter%"
echo [AUTO] 自动字幕匹配规则: "%s_filter%"
goto AUTO_BACK
:try_video
setlocal DisableDelayedExpansion
echo [WORK] 正在匹配视频...
for /f "tokens=1* delims==" %%a in ('set vidext[') do (
for /r "%v_path%" %%c in ("*%%~b") do (
if exist "%%~c" (
echo [%%~b] 在目录中找到文件"%%~nxc"
for /f "tokens=1* delims=^!" %%d in ("%%~nxc") do (
if not "%%~e"=="" (
echo [RBVP] 文件名中含有半角感叹号"!",正在转换...
call :Module_ReplaceBatch "%%~nxc" "!" "@insert@"
for /f "tokens=1,2 delims=^| usebackq" %%f in ("%USERPROFILE%\rforbat.log") do (
echo "%%~f">>"%rf%%%~a.log"
set "insert_v=%%~g"
)
) else echo "%%~nxc">>"%rf%%%~a.log"
)
)
)
if not exist "%rf%%%~a.log" (
set "count_v_%%~a=0"
echo;>"%rf%%%~a.log"
)
)
setlocal EnableDelayedExpansion
set "log_count=0"
for /r "%rf%" %%a in ("*.log") do set /a log_count=log_count+1
if not %log_count% GTR 0 (
echo [ERRA] 找不到视频
goto MAIN
)
if not defined ext_use_most_v goto v_non_usemost
for /r "%rf%" %%a in ("*.log") do (
echo [CONT] 正在计数"%%~a"
for /f "usebackq tokens=*" %%b in ("%%~a") do (
call :number_list "log_v_%%~na" "%%~b"
set /a count_v_%%~na=count_v_%%~na+1
)
)
echo [LARG] 正在列表中寻找数量最多的扩展名
for /f "tokens=*" %%a in ('set /a log_count-1') do (
if %%~a GTR 0 (
call :list_largest "count_v_vidext" "%%~a"
) else (
for /f "tokens=1* delims==" %%b in ('set count_v_vidext[') do (
set "l_largest=%%~b"
)
)
)
goto v_ext_end
:v_non_usemost
for /r "%rf%" %%a in ("*.log") do (
echo [CONT] 正在计数"%%~a"
for /f "usebackq tokens=*" %%b in ("%%~a") do (
call :number_list "log_v_vidext[0]" "%%~b"
)
)
set "l_largest=count_v_vidext[0]"
:v_ext_end
if not defined same_long_v goto v_non_samelong
set "largest_vid=%l_largest:~14%"
echo [LSTR] 正在计算文件名长度
for /f "tokens=1* delims==" %%a in ('set log_v_%l_largest:~8%') do (
call :cut_string "%%~b" "%%~a"
)
echo [NAPR] 正在选择出现次数最多的文件名长度
call :list_appear "long_v%largest_vid%"
call :list_appear_most "long_v%largest_vid%" "num_appear"
echo [LIST] 正在列表文件名
for /f "tokens=1* delims==" %%a in ('set long_v') do (
if "%%~b"=="%appear_most%" (
call :cut_string_2 "%%~a"
)
)
goto v_list_end
:v_non_samelong
echo [LIST] 正在列表文件名
for /f "tokens=1* delims==" %%a in ('set log_v_%l_largest:~8%') do (
call :cut_string_3 "%%~a"
)
:v_list_end
echo [COMP] 列表内差异计算
call :compare_list "c_v" "%count_range%"
call :list_appear "comp_diff"
call :list_appear_most "comp_diff" "num_appear"
echo [CHKN] 容差值计算 ±2
for /f "tokens=1* delims==" %%a in ('set comp_diff%l_largest:~12%') do (
for /f "tokens=1,2 delims=," %%c in ("%%~b") do (
for /f "tokens=1* delims==" %%e in ('set comp_diff') do (
for /f "tokens=1,2 delims=," %%g in ("%%~f") do (
call :check_num "%%~g" "%%~c,2" "%%~e" "c_v"
call :check_num "%%~h" "%%~d,1" "%%~e" "c_v"
)
)
)
)
echo [LIST] 开始制表文件名
for /f "tokens=1* delims==" %%a in ('set comp_diff') do (
call :make_list "%%~a" "%%~b" "c_v" "9"
)
set "list_count=-1"
for /f "tokens=*" %%a in ('set v_diff') do (
set /a list_count=list_count+1
)
echo [LIST] 制表差异值
for /l %%a in (0,1,%list_count%) do (
for /f "tokens=1* delims==" %%b in ('set v_diff[%%~a]') do (
for /f "tokens=1,2,3 delims=," %%d in ("%%~c") do (
set "comp_diff_1[%%~a]=%%~d"
set "comp_diff_2[%%~a]=%%~e"
)
)
)
echo [MINI] 差异位置求最小值
call :list_minimum "comp_diff_1" "%list_count%"
echo [MINI] 差异位置"%n_minimum%",位于表"%l_minimum%"
echo [MAXN] 差异量求最大值
call :list_largest "comp_diff_2" "%list_count%"
echo [MAXN] 差异量"%n_largest%",位于表"%l_largest%"
set "vf_s=%n_minimum%"
set "vf_c=%n_largest%"
for /f "tokens=1,2,* delims=," %%a in ("%v_diff[0]%") do (
set "vf=%%~c"
set "vf_x=%%~xc"
goto v_dont_get_more
)
:v_dont_get_more
set "filter=!vf:~0,%vf_s%!"
call :add_char "?" "%vf_c%" "filter"
setlocal DisableDelayedExpansion
if defined insert_v (
call :Module_ReplaceBatch "%filter%" "%insert_v:~0,1%" "!"
for /f "tokens=* usebackq" %%a in ("%USERPROFILE%\rforbat.log") do set "filter=%%~a"
)
set "insert_v="
if defined ext_use_most_v (
set "filter=%filter%*%vf_x%"
) else set "filter=%filter%*"
echo "%filter%">>"%~dp0vf_rename.log"
echo [V_FI] 计算出视频过滤规则"%filter%"
setlocal EnableDelayedExpansion
call :clear_list "comp_diff_1"
call :clear_list "comp_diff_2"
call :clear_list "comp_diff"
call :clear_list "v_diff"
call :clear_list "count_v_vidext"
goto :EOF
:cut_string
set "cs_cut=%~2"
call :long_string "%~1" "s_long"
set "long_v%cs_cut:~12%=%s_long%"
goto :EOF
:cut_string_2
set "cs_cut=%~1"
set "cs_cut=!log_v_vidext%cs_cut:~6%!"
call :number_list "c_v" "%cs_cut%"
goto :EOF
:cut_string_3
set "cs_cut=%~1"
set "cs_cut=!log_v_vidext%cs_cut:~12%!"
call :number_list "c_v" "%cs_cut%"
goto :EOF
:make_list
set "in_name=%~1"
set "in_text=%~2"
set "in2_name=%~3"
set "instr_1=%~4"
set "count=!in_name:~%instr_1%!"
for /f "tokens=1* delims==" %%a in ('set %in2_name%%count%') do (
call :number_list "v_diff" "%in_text%,%%~b"
)
goto :EOF
:try_subtitles
echo [WORK] 正在匹配字幕...
setlocal DisableDelayedExpansion
for /f "tokens=1* delims==" %%a in ('set subext[') do (
for /r "%sub_path%" %%c in ("*%%~b") do (
if exist "%%~c" (
if "%%~xc"==".idx" (
if exist "%%~dpnc.sub" (
echo [%%~b] 在目录中找到文件"%%~nc" [IDX+SUB]
call :subtitles_remove_char "%%~c" "%%~a"
)
) else (
echo [%%~b] 在目录中找到文件"%%~nxc"
call :subtitles_remove_char "%%~c" "%%~a"
)
)
)
if not exist "%rf%%%~a.log" (
set "count_s_%%~a=0"
echo;>"%rf%%%~a.log"
)
)
goto s_remove_char_finish
:subtitles_remove_char
for /f "tokens=1* delims=^!" %%a in ("%~nx1") do (
if not "%%~b"=="" (
echo [RBSP] 文件名中含有半角感叹号"!",正在转换...
call :Module_ReplaceBatch "%~nx1" "!" "@insert@"
for /f "tokens=1,2 delims=^| usebackq" %%c in ("%USERPROFILE%\rforbat.log") do (
echo "%~1">>"%rf%%~2.log"
set "insert_s=%%~d"
)
) else echo "%~nx1">>"%rf%%~2.log"
)
goto :EOF
:s_remove_char_finish
setlocal EnableDelayedExpansion
set "log_count=0"
for /r "%rf%" %%a in ("*.log") do set /a log_count=log_count+1
if not %log_count% GTR 0 (
echo [ERRA] 找不到字幕
goto MAIN
)
if not defined ext_use_most_s goto s_non_usemost
for /r "%rf%" %%a in ("*.log") do (
echo [CONT] 正在计数"%%~a"
for /f "usebackq tokens=*" %%b in ("%%~a") do (
call :number_list "log_s_%%~na" "%%~b"
set /a count_s_%%~na=count_s_%%~na+1
)
)
echo [LARG] 正在列表中寻找数量最多的扩展名
for /f "tokens=*" %%a in ('set /a log_count-1') do (
if %%~a GTR 0 (
call :list_largest "count_s_subext" "%%~a"
) else (
for /f "tokens=1* delims==" %%b in ('set count_s_subext[') do (
set "l_largest=%%~b"
)
)
)
goto s_ext_end
:s_non_usemost
for /r "%rf%" %%a in ("*.log") do (
for /f "usebackq tokens=*" %%b in ("%%~a") do (
call :number_list "log_s_subext[0]" "%%~b"
)
)
set "l_largest=count_s_subext[0]"
:s_ext_end
if not defined same_long_s goto s_non_samelong
set "largest_sid=%l_largest:~14%"
echo [LSTR] 正在计算文件名长度
for /f "tokens=1* delims==" %%a in ('set log_s_%l_largest:~8%') do (
call :cut_string_s "%%~b" "%%~a"
)
echo [NAPR] 正在选择出现次数最多的文件名长度
call :list_appear "long_s%largest_sid%"
call :list_appear_most "long_s%largest_sid%" "num_appear"
echo [LIST] 正在列表文件名
for /f "tokens=1* delims==" %%a in ('set long_s') do (
if "%%~b"=="%appear_most%" (
call :cut_string_s_2 "%%~a"
)
)
goto s_list_end
:s_non_samelong
echo [LIST] 正在列表文件名
for /f "tokens=1* delims==" %%a in ('set log_s_%l_largest:~8%') do (
call :cut_string_s_3 "%%~a"
)
:s_list_end
echo [COMP] 列表内差异计算
call :compare_list "c_s" "%count_range%"
call :list_appear "comp_diff"
call :list_appear_most "comp_diff" "num_appear"
echo [CHKN] 容差值计算 ±2
for /f "tokens=1* delims==" %%a in ('set comp_diff%l_largest:~12%') do (
for /f "tokens=1,2 delims=," %%c in ("%%~b") do (
for /f "tokens=1* delims==" %%e in ('set comp_diff') do (
for /f "tokens=1,2 delims=," %%g in ("%%~f") do (
call :check_num "%%~g" "%%~c,2" "%%~e" "c_s"
call :check_num "%%~h" "%%~d,1" "%%~e" "c_s"
)
)
)
)
echo [LIST] 开始制表文件名
for /f "tokens=1* delims==" %%a in ('set comp_diff') do (
call :make_list_s "%%~a" "%%~b" "c_s" "9"
)
set "list_count=-1"
for /f "tokens=*" %%a in ('set s_diff') do (
set /a list_count=list_count+1
)
echo [LIST] 制表差异值
for /l %%a in (0,1,%list_count%) do (
for /f "tokens=1* delims==" %%b in ('set s_diff[%%~a]') do (
for /f "tokens=1,2,3 delims=," %%d in ("%%~c") do (
set "comp_diff_1[%%~a]=%%~d"
set "comp_diff_2[%%~a]=%%~e"
)
)
)
echo [MINI] 差异位置求最小值
call :list_minimum "comp_diff_1" "%list_count%"
echo [MINI] 差异位置"%n_minimum%",位于表"%l_minimum%"
echo [MAXN] 差异量求最大值
call :list_largest "comp_diff_2" "%list_count%"
echo [MAXN] 差异量"%n_largest%",位于表"%l_largest%"
set "sf_s=%n_minimum%"
set "sf_c=%n_largest%"
for /f "tokens=1,2,* delims=," %%a in ("%s_diff[0]%") do (
set "sf=%%~c"
set "sf_x=%%~xc"
goto s_dont_get_more
)
:s_dont_get_more
set "s_filter=!sf:~0,%sf_s%!"
call :add_char "?" "%sf_c%" "s_filter"
setlocal DisableDelayedExpansion
if defined insert_s (
call :Module_ReplaceBatch "%s_filter%" "%insert_s:~0,1%" "!"
for /f "tokens=* usebackq" %%a in ("%USERPROFILE%\rforbat.log") do set "s_filter=%%~a"
)
set "insert_s="
if defined ext_use_most_s (
set "s_filter=%s_filter%*%sf_x%"
) else set "s_filter=%s_filter%*"
echo "%s_filter%">>"%~dp0sf_rename.log"
echo [V_FI] 计算出字幕过滤规则"%s_filter%"
setlocal EnableDelayedExpansion
call :clear_list "comp_diff_1"
call :clear_list "comp_diff_2"
call :clear_list "comp_diff"
call :clear_list "count_s_subext"
call :clear_list "s_diff"
goto :EOF
:cut_string_s
set "cs_cut=%~2"
call :long_string "%~1" "s_long"
set "long_s%cs_cut:~12%=%s_long%"
goto :EOF
:cut_string_s_2
set "cs_cut=%~1"
set "cs_cut=!log_s_subext%cs_cut:~6%!"
call :number_list "c_s" "%cs_cut%"
goto :EOF
:cut_string_s_3
set "cs_cut=%~1"
set "cs_cut=!log_s_subext%cs_cut:~12%!"
call :number_list "c_s" "%cs_cut%"
goto :EOF
:make_list_s