【问题标题】:How to trim sound files in subfolders by using sox in Mac terminal?如何在 Mac 终端中使用 sox 修剪子文件夹中的声音文件?
【发布时间】:2018-01-12 23:02:52
【问题描述】:

我有超过 1000 个文件夹,每个文件夹包含一个 .wav 文件和一个 .txt 文件 (example of the folders)。文本文件包含时间间隔,我需要根据每个文本文件给定的时间间隔将 .wav 文件修剪成剪辑(请注意,文本文件位于不同的文件夹中)。我从enter link description here得到了以下脚本

#!/bin/bash
index=0
while read this; do
if [ $index -gt 0 ]; then
 sox sound.wav clip-$index.wav trim $start $this-$start
fi
((index+=1))
start=$this
done < times.txt

但是,它是针对单个文件的,它只能用于当前目录中的文件。如何使其适用于子文件夹并进入循环?

【问题讨论】:

  • done 是一个从未开始的循环的结束。试试shellcheck.net 找出最明显的错误。
  • 感谢本杰明,shellcheck 非常有用,但您并没有真正回答我的问题..
  • done 本身就是一个语法错误。您必须将其与while ...; do 配对。看看BashFAQ/001
  • 对不起,我想我在原来的问题中漏掉了一行,我刚刚编辑了它。但是,我怎样才能使它适用于子文件夹..?

标签: bash terminal sox


【解决方案1】:

基本上,您希望对所有子目录执行此循环。这意味着您需要遍历子目录:

#!/bin/bash
topdir=$(pwd)
find . -type d | while read dir ; do
    cd "$dir"
    if [  -f sound.wav -a -f times.txt ] ; then
        index=0
        while read this; do
            if [ $index -gt 0 ]; then
                sox sound.wav "clip-$index.wav" trim "$start" "$this-$start"
            fi
            ((index+=1))
            start="$this"
        done < times.txt
    fi
done

我假设您问题中的循环执行您希望它执行的操作,我只是添加了一些引号。

【讨论】:

  • 非常感谢@Ljm Dullaart。我刚刚将 sound.wavtimes.txt 更改为 *.wav*.txt ,但它只适用于第一个子文件夹,我收到一条错误消息,上面写着 line 6: [: too many arguments。知道为什么会这样吗?
  • 嗨@Ljm,我刚刚弄明白了。我实际上只需要在所有子目录中执行我的修剪命令。我刚刚添加了for dir in */; do (cd -- "$dir" &amp;&amp; trim command here) done。原始答案来自 [unix.stackexchange.com/questions/270477/… by cuonglm。
猜你喜欢
  • 1970-01-01
  • 2012-03-28
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多