Criando um loop for em volta do loop existente, para substituir a entrada do usuário

0

Basicamente tenho este script:

#!/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

O que funciona perfeitamente bem. O que ele faz: pesquisa o arquivo srt, que tem o mesmo nome, tem o arquivo mp4 para uma palavra-chave, depois encontra o registro de data e hora que corresponde a essa palavra-chave e corta o vídeo do ponto inicial até o ponto final.

Arquivo SRT, por exemplo:

**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

Então, basicamente, se você estiver procurando a palavra-chave "toekomst", ela produzirá dois mp4, um que originalmente começou em 00:00:00,000 e terminou em 00:00:04,950 e um que começou em 00:00:02,639 e terminou em 00:00:05,670 .

Eu tenho vários MP4's no mesmo diretório, todos com um arquivo .srt correspondente com o mesmo nome do mp4, que precisam ser executados através deste script. Por isso, quero criar extensões de script que procurem todos os arquivos com os mesmos nomes e executá-las pelo script.

Então eu escrevi este trecho de código para testar isso:

#!/bin/bash
cd "'dirname "$0"'"
for file in *.srt
do 
fileName="$( basename "$file" .srt)"
echo $fileName
echo $fileName.mp4
echo $fileName.srt

done >temp

e, de fato, fornece a saída de todos os arquivos .mp4 e arquivos .srt no diretório:

h
h.mp4
h.srt
r
r.mp4
r.srt

Então, eu construo esse loop para o código existente da seguinte forma:

#!/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

e para a primeira execução, com o primeiro arquivo, ele fornece a saída correta do mp4, mas depois disso ele embaralha as variáveis de alguma maneira que eu não consigo obter a saída adequada. Espero que vocês possam ajudar.

    
por tjallo 02.03.2018 / 14:30

1 resposta

0

Eu mesmo consertei usando outro script e exportando as variáveis para o script principal, então, no final, usei esses dois scripts. Roteiro principal:

#!/bin/bash
echo 'Welk woord zoek je?'
read word
export word

for file in *.srt
do

fileName="$( basename "$file" .srt)"
export fileName
./actualScript

done

Script real:

#!/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
    
por 02.03.2018 / 15:31