【已解决】srt字幕文件如何调整时间?
本帖最后由 chamlien 于 2014-8-9 12:43 编辑字幕文件如下:
1
00:00:00,001 --> 00:00:00,300
Subtitle created via greatdreamers.sinaapp.com/tedsubs
2
00:00:15,000 --> 00:00:18,000
Phyllis Rodriguez: We are here today
3
00:00:18,000 --> 00:00:20,000
because of the fact
4
00:00:20,000 --> 00:00:22,000
that we have what most people consider
5
00:00:22,000 --> 00:00:24,000
an unusual friendship.
6
00:00:24,000 --> 00:00:26,000
And it is.
7
00:00:26,000 --> 00:00:29,000
And yet, it feels natural to us now.
8
00:00:29,000 --> 00:00:31,000
I first learned
9
00:00:31,000 --> 00:00:35,000
that my son had been in the World Trade Center
10
00:00:35,000 --> 00:00:39,000
on the morning of September 11th, 2001.
11
00:00:39,000 --> 00:00:41,000
We didn't know
12
00:00:41,000 --> 00:00:43,000
if he had perished yet
13
00:00:43,000 --> 00:00:46,000
until 36 hours later.
14
00:00:48,000 --> 00:00:50,000
At the time,
15
00:00:50,000 --> 00:00:53,000
we knew that it was political.
16
00:00:53,000 --> 00:00:56,000
We were afraid of what our country was going to do
17
00:00:56,000 --> 00:00:58,000
in the name of our son --
18
00:00:58,000 --> 00:01:01,000
my husband, Orlando, and I and our family.
19
00:01:01,000 --> 00:01:03,000
And when I saw it --
20
00:01:03,000 --> 00:01:05,000
and yet, through the shock,
21
00:01:05,000 --> 00:01:07,000
the terrible shock,
22
00:01:07,000 --> 00:01:13,000
and the terrible explosion in our lives, literally,
23
00:01:13,000 --> 00:01:16,000
we were not vengeful.
24
00:01:16,000 --> 00:01:18,000
And a couple of weeks later
25
00:01:18,000 --> 00:01:21,000
when Zacarias Moussaoui was indicted
26
00:01:21,000 --> 00:01:26,000
on six counts of conspiracy to commit terrorism,
27
00:01:26,000 --> 00:01:29,000
and the U.S. government called for a death penalty
28
00:01:29,000 --> 00:01:31,000
for him, if convicted,
29
00:01:31,000 --> 00:01:34,000
my husband and I spoke out
30
00:01:34,000 --> 00:01:37,000
in opposition to that, publicly.
31
00:01:37,000 --> 00:01:39,000
Through that
32
00:01:39,000 --> 00:01:41,000
and through human rights groups,
33
00:01:41,000 --> 00:01:43,000
we were brought together
34
00:01:43,000 --> 00:01:46,000
with several other victims' families.
35
00:01:46,000 --> 00:01:49,000
When I saw Aicha in the media,
36
00:01:49,000 --> 00:01:52,000
coming over when her son was indicted,
37
00:01:52,000 --> 00:01:55,000
and I thought, "What a brave woman.
38
00:01:55,000 --> 00:01:58,000
Someday I want to meet that woman when I'm stronger."
39
00:01:58,000 --> 00:02:00,000
I was still in deep grief;
网上有这类的软件,但是请问AU3可以实现吗?比如整体提取三秒或者延后三秒
很想学习一下思路。 回复 1# chamlien
已自己解决,不过运行效率很低,占用CPU
先用批处理把字幕文件转换为txt(ren *srt *txt)
原理是先读取总行数,然后识别每行字符串前三位是否等于"00:"
如果是,即把原来前后的时间分别调整(需要时间格式转换),然后写入新的文本
再用批处理把txt文件转换为srt(ren *txt *srt) 回复 2# chamlien
自己再整合处理一下,最好添加个GUI.
关键部分(全部提前或延迟)已经实现了!#include <date.au3>
Local $str = FileRead('MY.SRT')
Local $s_re = StringRegExp($str, '(?m)((?:^\d+\h*\v+)\V+\v+\V+\v+)', 3)
If @error Then Exit MsgBox(16, 'ERR', '文件内容不符合要求?')
Local $str_new = ''
For $i = 0 To UBound($s_re) -1
Local $aTime = StringRegExp($s_re[$i], '\d{2}:\d{2}:\d{2}', 3)
If @error Then Exit MsgBox(16, 'ERR', '时间戳不对?')
Local $sTime1 = _get_new_time($aTime, 3) ; 延迟3秒
Local $sTime2 = _get_new_time($aTime, 3) ;提前时间 _get_new_time($aTime, -3)
Local $str_temp = StringRegExpReplace($s_re[$i], '(?m)^\d{2}:\d{2}:\d{2}', $sTime1)
$str_temp = StringRegExpReplace($str_temp, '(?m)(\d{2}:\d{2}:\d{2})(' & Chr(44) & '\d{3}\v+)', $sTime2 & '\2')
$str_new &= $str_temp & @CRLF
Next
MsgBox(0, '', $str_new)
Func _get_new_time($sTime, $iTime)
Local $date = _NowCalcDate()
Return StringRegExpReplace(_DateAdd('s', $iTime, $date & ' ' & $sTime), '.+\h', '')
EndFunc
本帖最后由 komaau3 于 2014-8-10 01:23 编辑
回复 1# chamlien
时间处理 最基本的数学运算 写了串代码仅供参考
注意:非最新版本AU3没FileReadToArray这个函数 可考虑使用file.au3里的_FileReadToArray
Local $aSrt = FileReadToArray("大话西游之仙履奇缘.srt")
For $i = 0 To UBound($aSrt)-1
$aSrt[$i] = chgTime($aSrt[$i], 500);延迟500毫秒, 提前则负数
Next
Local $sSrt = ""
For $i = 0 To UBound($aSrt)-1
$sSrt &= $aSrt[$i] & @CRLF
Next
FileWrite("New.srt", $sSrt)
;ConsoleWrite(chgTime("00:00:00,001 --> 00:00:00,300", 500) & @CRLF)
;ConsoleWrite(chgTime("00:00:00,001 --> 00:00:00,300", -500) & @CRLF)
Func chgTime($sT, $nT)
Local $aMatch, $t1, $t2
$aMatch = StringRegExp($sT, "(\d{2}):(\d{2}):(\d{2}),(\d{3})", 3)
If @error Or UBound($aMatch) <> 8 Then Return $sT
$t1 = Int($aMatch)*3600000 + Int($aMatch)*60000 + Int($aMatch)*1000 + Int($aMatch)
$t1 = fmtTime($t1 + $nT)
$t2 = Int($aMatch)*3600000 + Int($aMatch)*60000 + Int($aMatch)*1000 + Int($aMatch)
$t2 = fmtTime($t2 + $nT)
Return $t1 & " --> " & $t2
EndFunc
Func fmtTime($n)
Local $n1, $n2, $n3, $n4
$n1 = Int($n/3600000)
$n2 = Int(Mod($n, 3600000)/60000)
$n3 = Int(Mod($n, 60000)/1000)
$n4 = Mod($n, 1000)
If $n1 < 0 Then $n1 = 0
If $n2 < 0 Then $n2 = 0
If $n3 < 0 Then $n3 = 0
If $n4 < 0 Then $n4 = 0
Return $n1 & ":" & $n2 & ":" & $n3 & "," & $n4
EndFunc
示例处理结果上张图把
回复 3# user3000
用了正则,比我的代码效率高多了!以下是我的思路, 不过占用CPU很高,试试你这个怎么样
For $i = 1 To $CountLines Step 1
Local $line = FileReadLine($file1, $i)
Local $str = StringMid($line, 1, 3)
If $str == "00:" Then
$time1 = StringMid($line, 1, 8)
$zheng = _DateDiff('s', "1970/01/01 00:00:00", "1970/01/01 " & $time1)
$miao = $zheng + $sec;以秒数显示,不是hh:mm:ss字样
$time2 = StringMid($line, 18, 8)
$zheng2 = _DateDiff('s', "1970/01/01 00:00:00", "1970/01/01 " & $time2)
$miao2 = $zheng2 + $sec;;以秒数显示,不是hh:mm:ss字样
Local $hhmmss1 = _Format_Time($miao)
Local $hhmmss2 = _Format_Time($miao2)
Local $tiaozheng=$hhmmss1&","&$haomiao&" --> "&$hhmmss2&","&$haomiao
FileWrite($file2, $tiaozheng & @CRLF)
Else
FileWrite($file2, $line & @CRLF)
EndIf
If $i == $CountLines Then ExitLoop
Next
FileClose($file1)
FileClose($file2) 回复 4# komaau3
这位兄台也是用了正则,而且时间转换代码看起来容易理解,测试一下看看效率如何 回复 5# chamlien
4楼komaau3兄比我利害多了. 我只会捡现成的东西来用,做不来数学运算!
5楼,你的代码不应该一行一行写进文件,而应该数据整合起来,一次性写入,这能提升很多效率! 回复 7# user3000
5楼,你的代码不应该一行一行写进文件,而应该数据整合起来,一次性写入,这能提升很多效率!
这句话很受用,谢谢! 回复 4# komaau3
大侠,返回的时间最好用stringformat函数整一下比较好。 回复 9# xms77
stringformat可以输出时间格式吗?如何现实呢? 回复 5# chamlien
涉及毫秒的计算 你使用时间日期函数不是直接忽略了毫秒 回复 9# xms77
嗯返回 StringFormat("%02d:%02d:%02d,%03d", $n1, $n2, $n3, $n4) 回复 4# komaau3
FileReadToArray果然很强大,十分高效,6w多行的文本,1分钟之内搞定!
页:
[1]