开发者

Two Bash/Applescript needed to read and manipulate text files within folder

开发者 https://www.devze.com 2023-04-12 16:08 出处:网络
I have a couple dozen text (.txt) files containing long lists of words (one per line) that I want to convert into an audio file using OS X Lion \'Text to Speech.\' To do so, I need to add in synth voi

I have a couple dozen text (.txt) files containing long lists of words (one per line) that I want to convert into an audio file using OS X Lion 'Text to Speech.' To do so, I need to add in synth voice markup tags to control speech timing.

Script 1

Here is the format of what I have in .txt files:

Word1
Word2
Word3

Here is what I need to create the audio file:

Word1
[[slnc 600]]

[[slnc 900]]
Word1

[[slnc 3000]]

Word2
[[slnc 600]]

[[slnc 900]]
Word2

[[slnc 3000]]

Word3
[[slnc 600]]

[[slnc 900]]
Word3

[[slnc 3000]]

...etc,

The text files are on my Desktop in a folder called 'Words.' If possible, it would be great if the script could 开发者_运维百科be pointed towards this folder and told to iterate through each .txt file within, performing the changes described above.

Script 2

This one needs to read in tab-delimited words/phrases from .txt files within a Desktop folder called 'French.' Here is the tab-delimited .txt file format:

FrenchWord/Phrase1   EnglishWord/Phrase1
FrenchWord/Phrase2   EnglishWord/Phrase2

...etc,

And then output as:

say "FrenchWord/Phrase1" using "Thomas"
delay 3
say "EnglishWord/Phrase1" using "Daniel"

delay 5

say "FrenchWord/Phrase2" using "Thomas"
delay 3
say "EnglishWord/Phrase2" using "Daniel"

delay 5

...etc,

Since the .txt input files in this case contain both single words and phrases, I'm guessing that the script will need to grab everything 'left-of-tab-delimiter' as French, and everything 'right-of-tab-delimiter' as 'English.'

Any assistance would be immensely appreciated!:)

Cheers,

Dave


$ cat words.txt 
Word1
Word2
Word3
$ ./script1 words.txt # will produce words-with-timings.txt
$ cat words-with-timings.txt 
Word1
[[slnc 600]]

[[slnc 900]]
Word1

[[slnc 3000]]

Word2
[[slnc 600]]

[[slnc 900]]
Word2

[[slnc 3000]]

Word3
[[slnc 600]]

[[slnc 900]]
Word3

[[slnc 3000]]

$ cat phrases.txt 
FrenchWord/Bon jour EnglishWord/Good day
FrenchWord/Bon mot  EnglishWord/Well met
$ ./script2 phrases.txt # will produce phrases-with-timings.txt
$ cat phrases-with-timings.txt 
say "FrenchWord/Bon jour" using Thomas
delay 3
say "EnglishWord/Good day" using Daniel

delay 5

say "FrenchWord/Bon mot" using Thomas
delay 3
say "EnglishWord/Well met" using Daniel

delay 5

Script1:

#!/bin/bash

for wordfile_txt in "$@"
do

  wordfile_with_timings_txt=`echo $wordfile_txt | sed s/.txt/-with-timings.txt/`

  # Refuse to overwrite
  if [[ "$wordfile_txt" == "$wordfile_with_timings_txt" ]]
  then
    echo ".txt files only pls"
    exit 1
  fi

  while read word
  do
    echo $word
    echo '[[slnc 600]]'
    echo
    echo '[[slnc 900]]'
    echo $word
    echo
    echo '[[slnc 3000]]'
    echo
  done < $wordfile_txt > $wordfile_with_timings_txt

done

Script2:

#!/bin/bash

for phrasefile_txt in "$@"
do

  phrasefile_with_timings_txt=`echo $phrasefile_txt | sed s/.txt/-with-timings.txt/`

  # Refuse to overwrite
  if [[ "$phrasefile_txt" == "$phrasefile_with_timings_txt" ]]
  then
    echo ".txt files only pls"
    exit 1
  fi

  while read line
  do
    phrase1="`echo "$line" | cut -f 1`"
    phrase2="`echo "$line" | cut -f 2`"

    echo say \"$phrase1\" using "Thomas"
    echo delay 3
    echo say \"$phrase2\" using "Daniel"
    echo
    echo delay 5
    echo
  done < $phrasefile_txt > $phrasefile_with_timings_txt

done

To run these in batch I suggest using find and xargs:

$ find lotta-words -type f
lotta-words/words1.txt
lotta-words/words2.txt
lotta-words/words3.txt
$ find lotta-words -type f | xargs ./script1 
$ find lotta-words -type f
lotta-words/words1-with-timings.txt
lotta-words/words1.txt
lotta-words/words2-with-timings.txt
lotta-words/words2.txt
lotta-words/words3-with-timings.txt
lotta-words/words3.txt
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号