我需要一些关于 SRT 字幕文本处理的想法
I need some idea on text processing for SRT subtitles
标题说出了我真正需要的 ATM。
基本上我已经创建了一个基于 Tesseract 和 ImageMagick 的 OCR 工具链。我已经设法达到输出文本非常一致的程度。我正在使用它来对一些旧的硬字幕视频进行 OCR,并将它们制作成软字幕 SRT 字幕。为了截取图像输入的屏幕截图,我使用了我很久以前发现并重写的旧 shell 脚本的修改版本。那些被输入到第二个脚本中,该脚本将它们处理成 Tessaract 可读的形式。在这一点上,我可以轻松地手工完成剩余的工作,但如果可能的话,我想自动化除最终校对之外的所有工作。
示例文本(来自当前项目)
03:04.418 Their parents have always written letters thanking us. =
03:05.018 Their parents have always written letters thanking us. =
03:05.619 Their parents have always written letters thanking us. =
03:06.219 Their parents have always written letters thanking us. =
03:06.820 Their parents have always written letters thanking us. =
03:07.421 Their parents have always written letters thanking us. =
03:08.021 Their parents have always written letters thanking us. =
03:08.622 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:09.222 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:09.823 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:10.424 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:11.024 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:11.625 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:12.225 In additien te all the previeus requests se far..."
03:12.826 In additien te all the previeus requests se far..."
03:13.427 In additien te all the previeus requests se far..."
03:14.027 In additien te all the previeus requests se far..."
03:14.628 In additien te all the previeus requests se far..."
基本上我想匹配文本并从第一行和最后一行中提取时间戳并将它们设置为 srt 格式
1
00:03:04,418 --> 00:03:08,021
Their parents have always written
letters thanking us. =
2
00:03:08,622 --> 00:03:08,622
This seminary was highly reeemmended
| am relieved te leave her in your care. =
3
00:03:12,225 --> 00:03:14,628
In additien te all the previeus requests se far..."
在这一点上,我可以接受它作为一个单独的脚本。
基本上sub.txt在sub.srt出来。然后进行校对。现在在检测到的文本中有一点可变性,但它是最小的。 I 偶尔会被检测为 |
或 [
,并且有时会在一些奇怪的极端情况下混淆 o 和 e。
编辑 2020 年 2 月 2 日:
我进行了一些更改和调整以进一步获得我想要的。我的 shell 脚本和 Ivans。我已经删除了由 ivans 脚本和我的脚本生成的空白子行。
更新处理和 ocr 脚本顺便说一句
#!/bin/bash -x
cd ""
mkdir ocr
for f in *.png ;
do
base="$(basename "$f" | cut -d "." -f 1,2)"
echo "$base"
if [[ -z "" ]] ;
then
tran="$(convert "$f" -separate -average -crop +0+720 -threshold 11% -fill black -draw 'color 700,10 floodfill' +repage ocr/"$base".png)"
else
tran="$(convert "$f" -separate -average -crop +0+720 -negate -threshold 15% -fill white -draw 'color 700,10 floodfill' +repage ocr/"$base".png)"
fi
$tran
cd ocr
magick mogrify -pointsize 50 -fill blue -draw 'text 1400,310 "L" ' +repage "$base".png
cd ..
done
cd ocr
for i in *.png ;
do base2="$(basename "$i" | cut -d "." -f 1,2 | cut -d ":" -f 2,3)"
tesseract "$i" stdout -c page_separator='' --psm 6 --oem 1 --dpi 300 | { tr '\n' ' '; tr -s [:space:] ' '; echo; } >> text.txt
echo "$base2"" " >> time.txt
done
awk '{printf ("%s", [=12=]); getline < "text.txt"; print [=12=] }' time.txt >> out.txt
sed -i 's/|/I/g' out.txt
sed -i 's/\[/I/g' out.txt
#sed -i 's/L//g' out.txt
#sed -i 's/=//g' out.txt
sed -i 's/.$//' out.txt
sed -i 's/.$//' out.txt
while read line ; do
sed "/[[:alpha:]]/ !d" >> sub.txt
done <out.txt
exit
制作蓝色 L 的部分是为了确保每一行都包含用于时间戳匹配的内容。
更新了 IVAN SRT 脚本
#!/bin/bash -x
sub="" # path to sub file
OLD=$IFS # remember current delimiter
IFS=$'\n' # set delimiter to the new line
raw=( $(cat $sub) ) # load sub into raw array
IFS=$OLD # set default delimiter back
reset () {
unset raw[0] # remove 1-st item from array
raw=( "${raw[@]}" ) # rearange array
}
output () {
printf "00:$time1 --> 00:$time3\n$text1\n\n"
}
speen () {
time3=$time2
reset
test=( "${raw[@]::2}" ) # get two more items
test2=( ${test[0]} ) # split 2-nd item
time2=${test2[0]} # get 2-nd timing
text2=${test2[@]:1} # get 2-nd text
# if only one item in test than this is the end, return
[[ "${test[1]}" ]] || { printf "00:$time1 --> 00:$time2\n$text1\n\n"; raw=; return; }
# compare, speen more if match, print ang go further if not
[[ "$text1" == "$text2" ]] && speen || output
}
N=1 # set counter
while [[ "${raw[@]}" ]]; do # loop through data
echo $((N++)) # print and inc counter
test1=( $raw ) # get 1-st item
time1=${test1[0]} # get 1-st timing
text1=${test1[@]:1}
# get 1-st text
speen
done
我刚刚添加了第三个时间变量来将旧的 time2 值保存为 time3。基本上消除空白时间戳行破坏了他的匹配。我意识到 time2 是第一个不匹配的时间戳。所以我需要保存最后一个循环之前的那个。因此 time3=$time2
然后休息 time2 值。然后使用旧时间2(现在时间3)打印子字符串。
以此结束
#!/bin/bash
sub=file # path to sub file
OLD=$IFS # remember current delimiter
IFS=$'\n' # set delimiter to the new line
raw=( $(cat $sub) ) # load sub into raw array
IFS=$OLD # set default delimiter back
reset () {
unset raw[0] # remove 1-st item from array
raw=( "${raw[@]}" ) # rearange array
}
output () {
text1=${text1//|/I} # change | to I in text
text1=${text1//[/I} # change [ to I in text
printf "$time1 --> $time2\n$text1\n\n"
}
speen () {
reset
test=( "${raw[@]::2}" ) # get two more items
test2=( ${test[0]} ) # split 2-nd item
time2=${test2[0]} # get 2-nd timing
text2=${test2[@]:1} # get 2-nd text
# if only one item in test than this is the end, return
[[ "${test[1]}" ]] || { printf "$time1 --> $time2\n$text1\n\n"; raw=; return; }
# compare, speen more if match, print ang go further if not
[[ "$text1" == "$text2" ]] && speen || output
}
N=1 # set counter
while [[ "${raw[@]}" ]]; do # loop through data
echo $((N++)) # print and inc counter
test1=( $raw ) # get 1-st item
time1=${test1[0]} # get 1-st timing
text1=${test1[@]:1} # get 1-st text
speen
done
标题说出了我真正需要的 ATM。
基本上我已经创建了一个基于 Tesseract 和 ImageMagick 的 OCR 工具链。我已经设法达到输出文本非常一致的程度。我正在使用它来对一些旧的硬字幕视频进行 OCR,并将它们制作成软字幕 SRT 字幕。为了截取图像输入的屏幕截图,我使用了我很久以前发现并重写的旧 shell 脚本的修改版本。那些被输入到第二个脚本中,该脚本将它们处理成 Tessaract 可读的形式。在这一点上,我可以轻松地手工完成剩余的工作,但如果可能的话,我想自动化除最终校对之外的所有工作。
示例文本(来自当前项目)
03:04.418 Their parents have always written letters thanking us. =
03:05.018 Their parents have always written letters thanking us. =
03:05.619 Their parents have always written letters thanking us. =
03:06.219 Their parents have always written letters thanking us. =
03:06.820 Their parents have always written letters thanking us. =
03:07.421 Their parents have always written letters thanking us. =
03:08.021 Their parents have always written letters thanking us. =
03:08.622 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:09.222 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:09.823 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:10.424 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:11.024 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:11.625 This seminary was highly reeemmended. | am relieved te leave her in your care. =
03:12.225 In additien te all the previeus requests se far..."
03:12.826 In additien te all the previeus requests se far..."
03:13.427 In additien te all the previeus requests se far..."
03:14.027 In additien te all the previeus requests se far..."
03:14.628 In additien te all the previeus requests se far..."
基本上我想匹配文本并从第一行和最后一行中提取时间戳并将它们设置为 srt 格式
1
00:03:04,418 --> 00:03:08,021
Their parents have always written
letters thanking us. =
2
00:03:08,622 --> 00:03:08,622
This seminary was highly reeemmended
| am relieved te leave her in your care. =
3
00:03:12,225 --> 00:03:14,628
In additien te all the previeus requests se far..."
在这一点上,我可以接受它作为一个单独的脚本。
基本上sub.txt在sub.srt出来。然后进行校对。现在在检测到的文本中有一点可变性,但它是最小的。 I 偶尔会被检测为 |
或 [
,并且有时会在一些奇怪的极端情况下混淆 o 和 e。
编辑 2020 年 2 月 2 日:
我进行了一些更改和调整以进一步获得我想要的。我的 shell 脚本和 Ivans。我已经删除了由 ivans 脚本和我的脚本生成的空白子行。
更新处理和 ocr 脚本顺便说一句
#!/bin/bash -x
cd ""
mkdir ocr
for f in *.png ;
do
base="$(basename "$f" | cut -d "." -f 1,2)"
echo "$base"
if [[ -z "" ]] ;
then
tran="$(convert "$f" -separate -average -crop +0+720 -threshold 11% -fill black -draw 'color 700,10 floodfill' +repage ocr/"$base".png)"
else
tran="$(convert "$f" -separate -average -crop +0+720 -negate -threshold 15% -fill white -draw 'color 700,10 floodfill' +repage ocr/"$base".png)"
fi
$tran
cd ocr
magick mogrify -pointsize 50 -fill blue -draw 'text 1400,310 "L" ' +repage "$base".png
cd ..
done
cd ocr
for i in *.png ;
do base2="$(basename "$i" | cut -d "." -f 1,2 | cut -d ":" -f 2,3)"
tesseract "$i" stdout -c page_separator='' --psm 6 --oem 1 --dpi 300 | { tr '\n' ' '; tr -s [:space:] ' '; echo; } >> text.txt
echo "$base2"" " >> time.txt
done
awk '{printf ("%s", [=12=]); getline < "text.txt"; print [=12=] }' time.txt >> out.txt
sed -i 's/|/I/g' out.txt
sed -i 's/\[/I/g' out.txt
#sed -i 's/L//g' out.txt
#sed -i 's/=//g' out.txt
sed -i 's/.$//' out.txt
sed -i 's/.$//' out.txt
while read line ; do
sed "/[[:alpha:]]/ !d" >> sub.txt
done <out.txt
exit
制作蓝色 L 的部分是为了确保每一行都包含用于时间戳匹配的内容。
更新了 IVAN SRT 脚本
#!/bin/bash -x
sub="" # path to sub file
OLD=$IFS # remember current delimiter
IFS=$'\n' # set delimiter to the new line
raw=( $(cat $sub) ) # load sub into raw array
IFS=$OLD # set default delimiter back
reset () {
unset raw[0] # remove 1-st item from array
raw=( "${raw[@]}" ) # rearange array
}
output () {
printf "00:$time1 --> 00:$time3\n$text1\n\n"
}
speen () {
time3=$time2
reset
test=( "${raw[@]::2}" ) # get two more items
test2=( ${test[0]} ) # split 2-nd item
time2=${test2[0]} # get 2-nd timing
text2=${test2[@]:1} # get 2-nd text
# if only one item in test than this is the end, return
[[ "${test[1]}" ]] || { printf "00:$time1 --> 00:$time2\n$text1\n\n"; raw=; return; }
# compare, speen more if match, print ang go further if not
[[ "$text1" == "$text2" ]] && speen || output
}
N=1 # set counter
while [[ "${raw[@]}" ]]; do # loop through data
echo $((N++)) # print and inc counter
test1=( $raw ) # get 1-st item
time1=${test1[0]} # get 1-st timing
text1=${test1[@]:1}
# get 1-st text
speen
done
我刚刚添加了第三个时间变量来将旧的 time2 值保存为 time3。基本上消除空白时间戳行破坏了他的匹配。我意识到 time2 是第一个不匹配的时间戳。所以我需要保存最后一个循环之前的那个。因此 time3=$time2
然后休息 time2 值。然后使用旧时间2(现在时间3)打印子字符串。
以此结束
#!/bin/bash
sub=file # path to sub file
OLD=$IFS # remember current delimiter
IFS=$'\n' # set delimiter to the new line
raw=( $(cat $sub) ) # load sub into raw array
IFS=$OLD # set default delimiter back
reset () {
unset raw[0] # remove 1-st item from array
raw=( "${raw[@]}" ) # rearange array
}
output () {
text1=${text1//|/I} # change | to I in text
text1=${text1//[/I} # change [ to I in text
printf "$time1 --> $time2\n$text1\n\n"
}
speen () {
reset
test=( "${raw[@]::2}" ) # get two more items
test2=( ${test[0]} ) # split 2-nd item
time2=${test2[0]} # get 2-nd timing
text2=${test2[@]:1} # get 2-nd text
# if only one item in test than this is the end, return
[[ "${test[1]}" ]] || { printf "$time1 --> $time2\n$text1\n\n"; raw=; return; }
# compare, speen more if match, print ang go further if not
[[ "$text1" == "$text2" ]] && speen || output
}
N=1 # set counter
while [[ "${raw[@]}" ]]; do # loop through data
echo $((N++)) # print and inc counter
test1=( $raw ) # get 1-st item
time1=${test1[0]} # get 1-st timing
text1=${test1[@]:1} # get 1-st text
speen
done