找回密码
 加入
搜索
查看: 11195|回复: 12

[AU3基础] 【已解决】srt字幕文件如何调整时间?

  [复制链接]
发表于 2014-8-8 23:51:21 | 显示全部楼层 |阅读模式
本帖最后由 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可以实现吗?比如整体提取三秒或者延后三秒

很想学习一下思路。
 楼主| 发表于 2014-8-9 12:42:52 | 显示全部楼层
回复 1# chamlien

已自己解决,不过运行效率很低,占用CPU

先用批处理把字幕文件转换为txt(ren *srt *txt)

原理是先读取总行数,然后识别每行字符串前三位是否等于"00:"

如果是,即把原来前后的时间分别调整(需要时间格式转换),然后写入新的文本

再用批处理把txt文件转换为srt(ren *txt *srt)
发表于 2014-8-9 23:49:10 | 显示全部楼层
回复 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[0], 3) ; 延迟3秒
        Local $sTime2 = _get_new_time($aTime[1], 3) ;提前时间 _get_new_time($aTime[0], -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
发表于 2014-8-10 01:19:13 | 显示全部楼层
本帖最后由 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[0])*3600000 + Int($aMatch[1])*60000 + Int($aMatch[2])*1000 + Int($aMatch[3])
        $t1 = fmtTime($t1 + $nT)
        
        $t2 = Int($aMatch[4])*3600000 + Int($aMatch[5])*60000 + Int($aMatch[6])*1000 + Int($aMatch[7])
        $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


示例处理结果上张图把

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2014-8-10 18:55:31 | 显示全部楼层
回复 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)
 楼主| 发表于 2014-8-10 18:56:53 | 显示全部楼层
回复 4# komaau3

这位兄台也是用了正则,而且时间转换代码看起来容易理解,测试一下看看效率如何
发表于 2014-8-10 20:12:58 | 显示全部楼层
回复 5# chamlien


    4楼komaau3兄比我利害多了. 我只会捡现成的东西来用,做不来数学运算!

5楼,你的代码不应该一行一行写进文件,而应该数据整合起来,一次性写入,这能提升很多效率!
 楼主| 发表于 2014-8-10 20:27:18 | 显示全部楼层
回复 7# user3000

5楼,你的代码不应该一行一行写进文件,而应该数据整合起来,一次性写入,这能提升很多效率!

这句话很受用,谢谢!
发表于 2014-8-10 22:56:23 | 显示全部楼层
回复 4# komaau3
大侠,返回的时间最好用stringformat函数整一下比较好。
 楼主| 发表于 2014-8-10 23:20:09 | 显示全部楼层
回复 9# xms77

stringformat可以输出时间格式吗?如何现实呢?
发表于 2014-8-11 13:35:33 | 显示全部楼层
回复 5# chamlien

涉及毫秒的计算 你使用时间日期函数不是直接忽略了毫秒
发表于 2014-8-11 13:44:45 | 显示全部楼层
回复 9# xms77

嗯  返回 StringFormat("%02d:%02d:%02d,%03d", $n1, $n2, $n3, $n4)
 楼主| 发表于 2014-8-11 19:30:43 | 显示全部楼层
回复 4# komaau3

FileReadToArray果然很强大,十分高效,6w多行的文本,1分钟之内搞定!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-29 08:13 , Processed in 0.097976 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表