I know the command for trimming is sox input output trim <start> <duration>
. But how do I convert all the .wav files in a folder into 1 second audios?
Asked
Active
Viewed 1,517 times
0

Saad
- 159
- 1
- 2
- 14
-
I just want one file. Not two or three separate files. Just one file of 1 second audio. If the length of the audio files are 5 seconds, I want all the files of to be trimmed down to 1 second. – Saad Jul 09 '18 at 18:25
-
did below solution solve your question ? – Scott Stensland Jul 11 '18 at 12:58
1 Answers
2
following script will read each file of input dir and use sox to make a copy of just the first 1 second of each wav file and put output files into supplied output dir ... nice aspect is it handles file names with spaces
#!/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 )
amend that last line of script if you need something other than just wav file types ... input files are not changed

Scott Stensland
- 26,870
- 12
- 93
- 104
-
This doesn't make the copy of first 1 second. I ran the script. The file sizes are smaller now but the length of the audios are still 2 second and 3 second. Not one second. – Saad Jul 10 '18 at 11:11
-
-
its working fine over here and the trim is correctly limiting output to 1 second ... on an output file issue this to confirm its 1 second ... sox output_file.wav -n stat 2>&1 | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p' – Scott Stensland Jul 10 '18 at 13:14
-
Is the command line statement format like this `./filename.sh inputWav outputWav`? – Saad Jul 10 '18 at 13:33
-
./filename.sh inputDirectory outputDirectory .... give it directory paths not file names – Scott Stensland Jul 10 '18 at 13:51