I am using pocketsphinx to convert audio file to text. These two commands must do the work:

First i run

ffmpeg -i file.mp3 -ar 16000 -ac 1 file.wav

then i run

pocketsphinx_continuous -infile file.wav 2> pocketsphinx.log > result.txt

I do that for each file (mp4 or mp3), I want to execute these two commands only once in a script, by browsing the directory content (files with the extension mp3 or mp4), and give the file the same name with the extension .txt (for exemple with the first command "file.mp3" become "file.wav" then using the second command "file.wav" become "file.txt").

How to do that using a single script file?

#!/usr/bin/env bash


for path in "$dir"/*.mp3; do
  name=$(basename "$path")
  ffmpeg -i "$path" -ar 16000 -ac 1 "$dir/$name.wav"
  pocketsphinx_continuous -infile "$dir/$name.wav" 2> "$dir/pocketsphinx.log" > "$dir/$name.txt"

Save the script somewhere like convert_all.sh and chmod +x it as well. Then use it like this:

./convert_all.sh path/to/some/dir

It will get all the .mp3s in that directory, convert them to .wav then run sphinx on them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.