我基本上有这个脚本:
#!/bin/bash
#Asks For filname and Word
echo 'Which word are you looking for?'
read word
echo 'What's the name of the file?'
read fileName
#Searches word and parses the line-numbers
wordOut=$(grep -i -n -w $word $fileName.srt |cut -f1 -d:)
#Sets all outputs to diffrent line numbers and saves a temp file
for word in $wordOut
do
echo $word
done >file.tmp
#Parses lines to array, removes temp file
mapfile -t arr <file.tmp
rm file.tmp
#Declares variable for the number of array entries (not used anywhere atm)
ln=${#arr[@]}
#Subtract all array entries with one
one=1
for i in "${arr[@]}"
do
crc=`expr $i - $one`
echo $crc
done >two.tmp
#Subtraction result to array2
mapfile -t arr2 <two.tmp
rm two.tmp
echo ${arr2[@]}
#retrieve times
for h in "${arr2[@]}"
do
line=$(sed "${h}q;d" $fileName.srt)
echo $line
done >three.tmp
#replace all commas with decimal points
sed 's/,/./g' three.tmp >four.tmp
#remove temp file 3 and parse 'decimal pointed' to array
rm three.tmp
mapfile -t arr3 <four.tmp
rm four.tmp
echo ${arr3[0]}
echo ${arr3[1]}
# converts HH:MM:SS.sss to fractional seconds
codes2seconds() (
local hh=${1%%:*}
local rest=${1#*:}
local mm=${rest%%:*}
local ss=${rest#*:}
printf "%s" $(bc <<< "$hh * 60 * 60 + $mm * 60 + $ss")
)
# converts fractional seconds to HH:MM:SS.sss
seconds2codes() (
local seconds=$1
local hh=$(bc <<< "scale=0; $seconds / 3600")
local remainder=$(bc <<< "$seconds % 3600")
local mm=$(bc <<< "scale=0; $remainder / 60")
local ss=$(bc <<< "$remainder % 60")
printf "%02d:%02d:%06.3f" "$hh" "$mm" "$ss"
)
subtracttimes() (
local t1sec=$(codes2seconds "$1")
local t2sec=$(codes2seconds "$2")
printf "%s" $(bc <<< "$t2sec - $t1sec")
)
for range in "${arr3[@]}"
do
mod=$(sed 's/[^0-9]//g' <<< $range)
duration=$(subtracttimes "${range%% -->*}" "${range##*--> }")
printf "%s\n" "ffmpeg -i $fileName.mp4 -ss ${range%% -->*} -t $duration -async 1 $word.$mod.$fileName.cut.mp4"
done >final.tmp
sudo chmod 755 final.tmp
./final.tmp
rm final.tmp
效果非常好。作用:在与mp4文件同名的srt文件中查找关键字,找到与该关键字匹配的时间戳,将视频从起点到终点进行剪切。
SRT文件,例如:
**video.srt**
1
00:00:00,000 --> 00:00:04,950
welkom bij eerste toekomst reizen dus
2
00:00:02,639 --> 00:00:05,670
onderdeel aan de achterhoekse toekomst
3
00:00:04,950 --> 00:00:07,290
stoere
4
00:00:05,670 --> 00:00:11,250
mijn heren nu al heel veel dingen
因此,基本上,如果您正在寻找关键字“toekomst”,它将输出两个 mp4,一个最初开始于 00:00:00,000
并结束于00:00:04,950
,另一个开始于00:00:02,639
并结束于00:00:05,670
。
我在同一目录中有多个 MP4,所有 MP4 都有一个与 mp4 同名的相应 .srt 文件,这些文件都需要通过此脚本运行。所以我想构建一个脚本扩展,它查找所有具有相同名称的文件,并通过脚本运行它。
所以我写了这段代码来测试一下:
#!/bin/bash
cd "`dirname "$0"`"
for file in *.srt
do
fileName="$( basename "$file" .srt)"
echo $fileName
echo $fileName.mp4
echo $fileName.srt
done >temp
它确实给出了目录中所有 .mp4 文件和 .srt 文件的输出:
h
h.mp4
h.srt
r
r.mp4
r.srt
然后我围绕现有代码构建这个 for 循环,如下所示:
#!/bin/bash
cd "`dirname "$0"`"
#Asks For filname and Word
echo 'Which word are you looking for?'
read word
for file in *.srt
do
fileName="$( basename "$file" .srt)"
#Searches word and parses the line-numbers
wordOut=$(grep -i -n -w $word $fileName.srt |cut -f1 -d:)
#Sets all outputs to diffrent line numbers and saves a temp file
for word in $wordOut
do
echo $word
done >file.tmp
#Parses lines to array, removes temp file
mapfile -t arr <file.tmp
rm file.tmp
#Declares variable for the number of array entries (not used anywhere atm)
ln=${#arr[@]}
#Subtract all array entries with one
one=1
for i in "${arr[@]}"
do
crc=`expr $i - $one`
echo $crc
done >two.tmp
#Subtraction result to array2
mapfile -t arr2 <two.tmp
rm two.tmp
echo ${arr2[@]}
#retrieve times
for h in "${arr2[@]}"
do
line=$(sed "${h}q;d" $fileName.srt)
echo $line
done >three.tmp
#replace all commas with decimal points
sed 's/,/./g' three.tmp >four.tmp
#remove temp file 3 and parse 'decimal pointed' to array
rm three.tmp
mapfile -t arr3 <four.tmp
rm four.tmp
echo ${arr3[0]}
echo ${arr3[1]}
# converts HH:MM:SS.sss to fractional seconds
codes2seconds() (
local hh=${1%%:*}
local rest=${1#*:}
local mm=${rest%%:*}
local ss=${rest#*:}
printf "%s" $(bc <<< "$hh * 60 * 60 + $mm * 60 + $ss")
)
# converts fractional seconds to HH:MM:SS.sss
seconds2codes() (
local seconds=$1
local hh=$(bc <<< "scale=0; $seconds / 3600")
local remainder=$(bc <<< "$seconds % 3600")
local mm=$(bc <<< "scale=0; $remainder / 60")
local ss=$(bc <<< "$remainder % 60")
printf "%02d:%02d:%06.3f" "$hh" "$mm" "$ss"
)
subtracttimes() (
local t1sec=$(codes2seconds "$1")
local t2sec=$(codes2seconds "$2")
printf "%s" $(bc <<< "$t2sec - $t1sec")
)
for range in "${arr3[@]}"
do
mod=$(sed 's/[^0-9]//g' <<< $range)
duration=$(subtracttimes "${range%% -->*}" "${range##*--> }")
printf "%s\n" "ffmpeg -i $fileName.mp4 -ss ${range%% -->*} -t $duration -async 1 $word.$mod.$fileName.cut.mp4"
done >final.tmp
sudo chmod 755 final.tmp
./final.tmp
rm final.tmp
done
对于第一次运行,第一个文件给出了正确的输出 mp4,但之后它以某种方式对变量进行洗牌,导致我无法获得正确的输出。
答案1
我自己通过使用另一个脚本并将变量导出到主脚本来修复它,所以最终我使用了这两个脚本。 主要脚本:
#!/bin/bash
echo 'Welk woord zoek je?'
read word
export word
for file in *.srt
do
fileName="$( basename "$file" .srt)"
export fileName
./actualScript
done
实际脚本:
#!/bin/bash
#Asks For filname and Word
#Searches word and parses the line-numbers
wordOut=$(grep -i -n -w $word $fileName.srt |cut -f1 -d:)
#Sets all outputs to diffrent line numbers and saves a temp file
for word in $wordOut
do
echo $word
done >file.tmp
#Parses lines to array, removes temp file
mapfile -t arr <file.tmp
rm file.tmp
#Declares variable for the number of array entries (not used anywhere atm)
ln=${#arr[@]}
#Subtract all array entries with one
one=1
for i in "${arr[@]}"
do
crc=`expr $i - $one`
echo $crc
done >two.tmp
#Subtraction result to array2
mapfile -t arr2 <two.tmp
rm two.tmp
echo ${arr2[@]}
#retrieve times
for h in "${arr2[@]}"
do
line=$(sed "${h}q;d" $fileName.srt)
echo $line
done >three.tmp
#replace all commas with decimal points
sed 's/,/./g' three.tmp >four.tmp
#remove temp file 3 and parse 'decimal pointed' to array
rm three.tmp
mapfile -t arr3 <four.tmp
rm four.tmp
echo ${arr3[0]}
echo ${arr3[1]}
# converts HH:MM:SS.sss to fractional seconds
codes2seconds() (
local hh=${1%%:*}
local rest=${1#*:}
local mm=${rest%%:*}
local ss=${rest#*:}
printf "%s" $(bc <<< "$hh * 60 * 60 + $mm * 60 + $ss")
)
# converts fractional seconds to HH:MM:SS.sss
seconds2codes() (
local seconds=$1
local hh=$(bc <<< "scale=0; $seconds / 3600")
local remainder=$(bc <<< "$seconds % 3600")
local mm=$(bc <<< "scale=0; $remainder / 60")
local ss=$(bc <<< "$remainder % 60")
printf "%02d:%02d:%06.3f" "$hh" "$mm" "$ss"
)
subtracttimes() (
local t1sec=$(codes2seconds "$1")
local t2sec=$(codes2seconds "$2")
printf "%s" $(bc <<< "$t2sec - $t1sec")
)
for range in "${arr3[@]}"
do
mod=$(sed 's/[^0-9]//g' <<< $range)
duration=$(subtracttimes "${range%% -->*}" "${range##*--> }")
printf "%s\n" "ffmpeg -i $fileName.mp4 -ss ${range%% -->*} -t $duration -async 1 $word.$mod.$fileName.cut.mp4"
done >final.tmp
sudo chmod 755 final.tmp
./final.tmp
rm final.tmp