ceskyDJ

MTS to AVI file converter (CLI)

Mar 31st, 2019
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.99 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Simple script for converting all MTS videos in directory or in all subdirectories using ffmpeg package
  4. # Author: ceskyDJ
  5. #
  6. # Dependencies: ffmpeg
  7. #
  8. # Prepare:
  9. # - Save to file (ex. /usr/local/bin/MTSToAVI)
  10. # - Set execute permission with chmod +x YourFile (ex. sudo chmod +x /usr/local/bin/MTSToAVI)
  11. #
  12. # Usage:
  13. # - For convert all files in specific directory:
  14. #     * Go to the directory (ex. cd ~/Videos/FromCamera)
  15. #     * Run script from saved file (ex. MTSToAVI)
  16. # - For convert all files in all subdirectories:
  17. #     * Go to the directory contains subdirectories with files
  18. #     * Run script from saved file (ex. MTSToAVI)
  19.  
  20. # Converts one file
  21. convertFile() {
  22.     # Rename to underscore name (fix for file names with spaces)
  23.     file="$1";
  24.     mv "$file" "${file// /_}";
  25.     file="${file// /_}";
  26.  
  27.     echo -e "\033[1;33mConverting file: '$1'...\033[0m";
  28.     ffmpeg -i $file -vcodec libxvid -b 18000k -acodec libmp3lame -ac 2 -ab 320k -deinterlace -s 1440x1080 `echo $file | cut -d'.' -f1`.avi;
  29.    
  30.     # Rename back to original
  31.     mv "$file" "${file//_/ }";
  32.    
  33.     # Rename new file
  34.     newFile="${file//.MTS/.avi}";
  35.     newFile="${newFile//.mts/.avi}";
  36.     mv "$newFile" "${newFile//_/ }";
  37.    
  38.     return;
  39. }
  40.  
  41. # Converts files in selected directory
  42. convertFilesInDirectory() {
  43.     for f in *.MTS; do
  44.         convertFile "$f";
  45.     done
  46.    
  47.     return;
  48. }
  49.  
  50. # Converts files in subdirectories of selected directory
  51. convertFilesInSubDirectories() {
  52.     # Move to subdirectory
  53.     cd "$1";
  54.     echo -e "\033[1;35mActual directory: '$1'\033[0m";
  55.  
  56.     convertFilesInDirectory;
  57.    
  58.     # Move back to parent directory
  59.     cd "..";
  60.    
  61.     return;
  62. }
  63.  
  64. # Search for subdirectories
  65. for d in */; do
  66.     # Subdirectories not found -> convert files in this directory
  67.     if [[ $d = "*/" ]]; then
  68.         convertFilesInDirectory;
  69.     # Subdirectories found -> convert their files
  70.     else
  71.         convertFilesInSubDirectories "$d";
  72.     fi
  73. done
  74.  
  75. exit
Add Comment
Please, Sign In to add comment