[Bash Shell] Script chuyển đổi hàng loạt file .wav sang file .raw

Trong quá trình nghiên cứu, tôi cần phải convert hơn 500 file .wav sang .raw bằng tiện ích SOX nên đã xây dựng script như sau:

Chú ý:

  • Tại câu hỏi Where is the parent-directory of wav-dir? bạn nên viết đường dẫn tuyệt đối đến thư mục chứa thư mục wav
  • Bạn nên chắc chắn rằng thư mục raw nếu đã tồn tại thì không chứa dữ liệu quan trọng trước khi chọn xoá nó

  1. #!/bin/bash
  2. echo “Where is the parent-directory of wav-dir?”
  3. read parent_wavDir
  4. cd $parent_wavDir
  5. if [ -d “raw” ]; then
  6.    echo “/raw exits”
  7.     echo “Delete all file in /raw (y/n?)”
  8.     read decision
  9.     if [[ $decision == “y” ]]; then
  10.         rm -Rf raw
  11.     fi
  12. else
  13.     echo “$parent_wavDir/raw doesn’t exits!”
  14.     echo “Make directory raw”
  15. fi
  16. echo “SOX will convert .wav file to .raw file. Do you want continue (y/n?)”
  17. read dec
  18. if [[ $dec == “y” ]]; then
  19.     mkdir raw
  20.     cd wav
  21.     echo “Copy all file from wav-directory to raw-directory”
  22.     for file in *.wav
  23.     do 
  24.         cp $file ../raw/$file
  25.     done
  26.     wait    
  27.     cd ../raw
  28.     for file in *.wav
  29.     do
  30.         echo “Convert $file to ${file%%.*}.raw”
  31.         sox $file -b 16 -e signed-integer -c 1 -r 48k -t raw ${file%%.*}.raw
  32.     done
  33.     wait    
  34.     rm *.wav
  35. fi

Bạn có thể tải file: Script Wav2Raw

hoan.ph

Leave a comment