标题: [文本处理] 请问用bat怎样实现批量分割字幕? [打印本页]
作者: zllk 时间: 2023-9-14 14:29 标题: 请问用bat怎样实现批量分割字幕?
本帖最后由 zllk 于 2023-9-14 18:15 编辑
“字幕”文件夹有以下文件:
01.srt
02.srt- 1
- 00:00:50,420 --> 00:00:54,000
- 字幕1
-
- 2
- 00:00:54,940 --> 00:00:58,899
- 字幕2
-
- 3
- 00:00:59,060 --> 00:01:03,736
- 字幕3
-
- 4
- 00:01:47,620 --> 00:01:49,611
- 字幕4
-
- 5
- 00:01:49,780 --> 00:01:54,217
- 字幕5
- ......
-
- 6
- 00:01:54,380 --> 00:01:56,450
- 字幕6
-
- 7
- 00:01:56,620 --> 00:02:01,057
- 字幕7
复制代码
执行批处理效果
01.P1.srt文本内容如下:- 1
- 00:00:50,420 --> 00:00:54,000
- 字幕1
-
- 2
- 00:00:54,940 --> 00:00:58,899
- 字幕2
-
- 3
- 00:00:59,060 --> 00:01:03,736
- 字幕3
复制代码
例如01.P2.srt文本内容如下:- 4
- 00:01:47,620 --> 00:01:49,611
- 字幕4
-
- 5
- 00:01:49,780 --> 00:01:54,217
- 字幕5
- ......
-
- 6
- 00:01:54,380 --> 00:01:56,450
- 字幕6
复制代码
例如01.P3.srt文本内容如下:- 7
- 00:01:56,620 --> 00:02:01,057
- 字幕7
复制代码
“字幕”文件夹有以下文件:
01.srt
01.P1.srt
01.P2.srt
01.P3.srt
02.srt
01.P1.srt
01.P2.srt
需求概括:能支持Unicode、ANSI、UTF-8编码字幕文件分割生成的字幕文件,中文没有乱码问题,效果如上所述
作者: hfxiang 时间: 2023-9-14 16:20
回复 1# zllk
可用第3方工具gawk ( http://bcn.bathome.net/tool/4.1.0/gawk.exe )来实现。
bat脚本方式如下:- @echo off
- cd /d "%~dp0"
- for /f "usebackq delims=" %%a IN (`dir/b *.srt^|findstr /RE /V "\.P[0-9][0-9]*\.srt"`) DO @awk -v"RS=\n[ \t]*\n" "FNR==1{p=0;Fn=substr(FILENAME,1,length(FILENAME)-4) \".P\";fe=\".srt\"}FNR%%3==1{p++}{print $0,ORS>(Fn P p fe)}" %%a
复制代码
命令行方式如下- for /f "usebackq delims=" %a IN (`dir/b *.srt^|findstr /RE /V "\.P[0-9][0-9]*\.srt"`) DO @awk -v"RS=\n[ \t]*\n" "FNR==1{p=0;Fn=substr(FILENAME,1,length(FILENAME)-4) \".P\";fe=\".srt\"}FNR%3==1{p++}{print $0,ORS>(Fn P p fe)}" %a
复制代码
作者: 77七 时间: 2023-9-14 17:06
有什么必要编辑1楼?现在根据1楼还能得出2楼的代码?
作者: qixiaobin0715 时间: 2023-9-14 17:21
- @echo off
- setlocal enabledelayedexpansion
- for /f "delims=" %%i in ('dir /b /a-d *.srt') do (
- set n=0
- for /f "delims=" %%j in (%%i) do (
- set /a n+=1
- set /a "m1=n%%3"
- set /a "m2=(n-1)/9+1"
- echo,%%j>>%%~ni.P!m2!.srt
- if "!m1!"=="0" echo,>>%%~ni.P!m2!.srt
- )
- )
- pause
复制代码
作者: 77七 时间: 2023-9-14 17:29
- @echo off
- for /f "delims=" %%i in ('dir /b /a-d *.srt') do (
- set /a n=0,m=1
- for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^<"%%i"') do (
- set "str=%%b"
- setlocal enabledelayedexpansion
- >>"%%~ni.P!m!.srt" echo=!str!
- endlocal
- if "%%b" equ "" (
- set /a n+=1,m=n/3+1
- )
- )
- )
- pause
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |