需要两个 Bash/Applescript 来读取和操作文件夹内的文本文件

发布于 2024-12-09 13:17:11 字数 1132 浏览 0 评论 0原文

我有几十个文本 (.txt) 文件,其中包含一长串单词(每行一个),我想使用 OS X Lion“文本转语音”将其转换为音频文件。为此,我需要添加合成语音标记标签来控制语音计时。

脚本 1

以下是 .txt 文件的格式:

Word1
Word2
Word3

以下是创建音频文件所需的内容:

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,

文本文件位于我的桌面上名为“Words”的文件夹中。如果可能的话,如果脚本可以指向此文件夹并被告知迭代其中的每个 .txt 文件,执行上述更改,那就太好了。

脚本 2

该脚本需要从名为“French”的桌面文件夹中的 .txt 文件中读取制表符分隔的单词/短语。以下是制表符分隔的 .txt 文件格式:

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

...etc,

然后输出为:

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,

由于本例中的 .txt 输入文件包含单个单词和短语,我猜测脚本需要获取“制表符左侧”的所有内容-delimiter' 为法语,所有 'right-of-tab-delimiter' 为 'English'。

任何帮助将不胜感激!:)

干杯,

戴夫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-12-16 13:17:11
$ 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

要批量运行这些,我建议使用 findxargs

$ 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
$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文