【问题标题】:Batch Processing using Sox使用 Sox 进行批处理
【发布时间】:2018-07-09 13:37:52
【问题描述】:

我知道修剪的命令是sox input output trim <start> <duration>。但是如何将文件夹中的所有 .wav 文件转换为 1 秒音频?

【问题讨论】:

  • 我只想要一个文件。不是两个或三个单独的文件。只有一个 1 秒音频的文件。如果音频文件的长度是 5 秒,我希望将所有文件修剪到 1 秒。
  • 以下解决方案是否解决了您的问题?

标签: shell ubuntu-16.04 sox audio-processing


【解决方案1】:

以下脚本将读取输入目录的每个文件,并使用 sox 复制每个 wav 文件的前 1 秒,并将输出文件放入提供的输出目录...很好的方面是它处理带有空格的文件名

#!/bin/bash

: << 'big_comment'
for each wav file in input dir make a copy of just its first 1 second
into output file in output dir keeping same file name in output ... this
does not pad with silence if file duration is < 1 second

    usage

    thisscript.sh  /input/audio/dir  /output_dir

big_comment

input_audio_dir="$1"
output_audio_dir="$2"

# ...

if [[ ! -d "$input_audio_dir" ]]; then

    echo "ERROR - failed to find dir input_audio_dir -->${input_audio_dir}<-- "
    exit 8
fi
if [[ ! -d "$output_audio_dir" ]]; then

    mkdir "$output_audio_dir"
fi
if [[ ! -d $output_audio_dir ]]; then

    echo "ERROR - failed to find dir output_audio_dir -->${output_audio_dir}<-- "
    exit 9
fi


while IFS='' read -r -d '' input_audio; do   #  read each wav file in input dir 

    : # now do something with "$input_audio"

    echo "input_audio  -->$input_audio<-- "

    just_input_filename=$( basename "${input_audio}" ) # get just filename not full path

    curr_output_file="${output_audio_dir}/${just_input_filename}"

    echo "curr_output_file  -->${curr_output_file}<-- "

    sox $input_audio $curr_output_file trim 0.0   00:00:01.00 || {   # ... catch error

        resp_code=$?
        echo
        echo "sox gave bad return code of $resp_code inside script $0 "
        exit  10
    }

done < <(find "${input_audio_dir}"  -iname \*.wav  -maxdepth 1 -type f -print0 )

如果您需要的不仅仅是 wav 文件类型,请修改最后一行脚本...输入文件不会更改

【讨论】:

  • 这不会复制前 1 秒。我运行了脚本。文件大小现在变小了,但音频的长度仍然是 2 秒和 3 秒。没有一秒钟。
  • 我不认为 sox 语句正在执行。
  • 它在这里工作正常,并且修剪正确地将输出限制为 1 秒...在输出文件上发出此问题以确认其 1 秒... sox output_file.wav -n stat 2>&1 | sed -n 's#^Length (秒):[^0-9]*([0-9.]*)$#\1#p'
  • 命令行语句格式是不是像./filename.sh inputWav outputWav这样的?
  • ./filename.sh inputDirectory outputDirectory .... 给它目录路径而不是文件名
猜你喜欢
  • 2015-01-31
  • 2016-12-27
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 2015-11-21
相关资源
最近更新 更多