ユーザー入力を置き換えるために、既存のループの周りにforループを構築します。

ユーザー入力を置き換えるために、既存のループの周りにforループを構築します。

デフォルトでは、次のスクリプトがあります。

#!/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」というキーワードを探している場合は、2つのmp4が出力されます。 1つは最初に始まり、 00:00:00,000終わり00:00:04,950、もう1つはで始まり、00:00:02,639終わります00:00:05,670

同じディレクトリに複数の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

おすすめ記事