【发布时间】:2018-06-17 17:28:41
【问题描述】:
我正在尝试创建一个 ruby 脚本,但我正在为如何继续而苦苦挣扎。
我的脚本的目标是,从 2 个字幕文件中,我想通过将第一个文件的时间与第二个文件的字幕相结合来制作第三个文件。
不知道我说清楚了没有,基本上我的2个字幕文件一样,只是版本不同,即相同序列的时间可能不同。 p>
这是想要的结果:
# File One : Version KILLERS (English)
1
00:00:01,874 --> 00:00:05,577
<i>Previously on "12 Monkeys"...</i>
2
00:00:05,625 --> 00:00:07,882
- Did Marion send you?
- Who?
3
00:00:07,938 --> 00:00:09,905
Marion, the boy's mother.
# File Two : Version AMZN (Translated in French)
1
00:00:08,140 --> 00:00:11,850
<i>Précédemment...</i>
2
00:00:12,260 --> 00:00:14,120
- C'est Marion qui vous envoie ?
- Qui ?
3
00:00:14,150 --> 00:00:16,110
Marion, sa mère.
# File Three : Objective -> Getting a KILLERS versions for French subtitles
1
00:00:01,874 --> 00:00:05,577
<i>Précédemment...</i>
2
00:00:05,625 --> 00:00:07,882
- C'est Marion qui vous envoie ?
- Qui ?
3
00:00:07,938 --> 00:00:09,905
Marion, sa mère.
我们可以为时间分配变量“time”(例如00:00:07,938),为文本分配变量“text”。
因此,我想用第二个文件中的时间替换第一个文件中的每个“时间线”。
这是我尝试过的:
#!/usr/bin/ruby -w
#To run the script : ./script.rb TVshowENG.srt TVshowESP.srt NewTVshow.srt
def time_srt (h, m, s, ms)
t = h + ":" + m + ":" + s + "," + ms
end
File.open(ARGV[0], 'r') do |file_one|
File.open(ARGV[1], 'r') do |file_two|
File.open(ARGV[2], 'w') do |file_out|
file_one.each_line do |line|
if line.strip =~ /^(\d{2}):(\d{2}):(\d{2}),(\d{3})\D+(\d{2}):(\d{2}):(\d{2}),(\d{3})$/
file_out << time_srt($1, $2, $3, $4) + ' --> ' +
time_srt($5, $6, $7, $8) + "\n"
else
file_out << line # I want to print line from file_two there
end
end
end
end
end
编辑:最终脚本随机故障
1 - 脚本运行问题:脚本运行,突然停止并出现此错误
./SyncScript.rb:25:in `block (4 levels) in <main>': invalid byte sequence in UTF-8 (ArgumentError)
from ./SyncScript.rb:18:in `loop'
from ./SyncScript.rb:18:in `block (3 levels) in <main>'
from ./SyncScript.rb:17:in `open'
from ./SyncScript.rb:17:in `block (2 levels) in <main>'
from ./SyncScript.rb:16:in `open'
from ./SyncScript.rb:16:in `block in <main>'
from ./SyncScript.rb:15:in `open'
from ./SyncScript.rb:15:in `<main>'
2- 故障(关于#的更多详细信息)
14 # From 1 to 16, it worked perfectly fine)
00:00:38,535 --> 00:00:40,832
Elle est revenue !
15
00:00:49,746 --> 00:00:51,620
<i>Ce que je vais te dire</i>
16
00:00:51,715 --> 00:00:54,749
<i>est la légende telle
que l'on me l'a racontée.</i>
00:01:01,650 --> 00:01:04,190 # Suddently, there's no sequence number (here, 17)
<i>Il y avait autrefois un serpent</i>
00:01:04,300 --> 00:01:06,870 # Same here (no 18)
<i>qui n'allait
que dans une seule direction.</i>
19 # Yet, there is 19. But no space ('\n' issue)
00:01:07,150 --> 00:01:10,740
<i>Toujours en avant, jamais en arrière.</i>
20 # Same...
00:01:11,020 --> 00:01:13,080
<i>Jusqu'au jour où,</i>
21
00:01:13,260 --> 00:01:16,770
<i>le serpent tomba sur un démon.</i>
22
00:01:21,670 --> 00:01:22,730
Stop !
23
00:01:38,050 --> 00:01:40,050
Ho ! Ho !
24
00:01:59,030 --> 00:02:01,090
Je suis sûr que vous ne serez pas
surpris d'apprendre que
25
00:02:01,130 --> 00:02:04,310
nous avons vu votre venue
26
00:02:06,380 --> 00:02:11,020 #No subtitles there ??
27
00:02:11,690 --> 00:02:13,390
【问题讨论】:
-
你说,“这是我试过的”。我们是否假设它没有产生预期的结果?你真的需要一个例子。我建议您提供两个字符串来显示这两个文件的内容,以及第三个字符串来显示您希望生成的文件的内容。为前两个字符串中的每一个分配一个变量会很有帮助(例如,
str1 = "..."、str2 ="...")。通过这样做,读者可以剪切和粘贴并在 cmets 和答案中引用这些变量,而无需定义它们。输入文件只需几行(越少越好)。 -
你在哪里说,“因此,我想替换每一个……”,你不是倒过来了吗?
-
我从翻译文件中得到它,但不是复制/粘贴时间,而是脚本会这样做。大约有 400 多个序列/集:D
-
我的意思是你不想用 first 文件中的时间替换 second 文件中的每个“时间线”吗?跨度>
-
这很重要,因为它是问题陈述的一部分;它解释了你想做什么。
标签: ruby