Advertisement
pa4080

merge-video-sub.bash

Jul 29th, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.39 KB | None | 0 0
  1. #!/bin/bash -e
  2.  
  3. # Check if all tools are available
  4. [ -x /usr/bin/notify-send ] || (echo "Please, install 'notify-send'"; exit 1)
  5. [ -x /usr/bin/mkvmerge ] || (echo "Please, install 'mkvmerge'"; exit 1)
  6.  
  7. # Allowed video and subtitle file extensions
  8. EXT_VIDEO=("mp4 avi mpg mov mkv wmv")
  9. EXT_SUB=("sub str srt vtt")
  10.  
  11. # Files, which names contains some of next strings will be removed in auto mode
  12. FILTER=("sample Sample SAMPLE")
  13.  
  14. # Log file
  15. MERGE_LOG="/tmp/merge-video-sub.log"
  16. echo > "$MERGE_LOG"
  17.  
  18. #
  19. # Functions
  20. #
  21.  
  22. function get-video-and-sub-file-names {
  23.     # Get the names of the video and subtitle files and move the rest of the files into the Backup directory
  24.     for ((i=0; i<${#FILE_LIST[@]}; i++)); do
  25.         FILE_NAME="${FILE_LIST[$i]%.*}"
  26.         FILE_EXT="${FILE_LIST[$i]##*.}"
  27.  
  28.         if   [[ "${EXT_SUB[@]}" == *"$FILE_EXT"* ]]; then
  29.             SUB_FULL_FILE_NAME="${FILE_LIST[$i]}"
  30.             SUB_FILE_NAME="${FILE_NAME}"
  31.             SUB_FILE_EXT="${FILE_EXT}"
  32.         elif [[ "${EXT_VIDEO[@]}" == *"$FILE_EXT"* ]]; then
  33.             VIDEO_FULL_FILE_NAME="${FILE_LIST[$i]}"
  34.             VIDEO_FILE_NAME="${FILE_NAME}"
  35.             VIDEO_FILE_EXT="${FILE_EXT}"
  36.         else
  37.             # We need 'find' to manipulate only with files, because "$BACKUP_DIR" is in the queue
  38.             find ./* -maxdepth 0 -type f -name "${FILE_LIST[$i]}" -exec mv "{}" "$BACKUP_DIR" \; -exec echo -e "The file {} was REMOVED.\n" >> "$MERGE_LOG" \;
  39.         fi
  40.     done
  41. }
  42.  
  43. function get-the-content-of-the-current-directory {
  44.     # Get the content of the current directory
  45.     shopt -s nullglob
  46.     FILE_LIST=(*)
  47.     shopt -u nullglob
  48. }
  49.  
  50. function mkvmerge-video-and-sub-files {
  51.     # Create merged file
  52.     mkvmerge -o "$OUTPUT_FILE" "$VIDEO_FULL_FILE_NAME" "$SUB_FULL_FILE_NAME"
  53.     sleep 3
  54. }
  55.  
  56. #
  57. # Scenario 1: If exactly two files are selected in Nautilus! Then check if they are 1 video and 1 subtitle files, if yes - merge and remove them
  58. # Scenario 2: Else run the standard procedure
  59. #
  60.  
  61. # Get the files, selected in Nautilus as file list. Use next command to check the result: notify-send "MESSAGE" "`echo -e "${#FILE_LIST[@]}"; printf '%s\n' "${FILE_LIST[@]}"`"
  62. IFS_BAK=$IFS
  63. IFS=$'\t\n'
  64. FILE_LIST=($NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)
  65. IFS=$IFS_BAK
  66.  
  67.  
  68. if [ "${#FILE_LIST[@]}" -eq "2" ]
  69. then # Scenario 1
  70.  
  71.     # Get the names of the video and subtitle files
  72.     get-video-and-sub-file-names
  73.  
  74.     if   [[ "${EXT_SUB[@]}" == *" $SUB_FILE_EXT "* ]] && [[ "${EXT_VIDEO[@]}" == *" $VIDEO_FILE_EXT "* ]]
  75.     then
  76.         notify-send "OK" "`echo -e "The following files will be MERGED and MOVED to trash:"; printf '\t-\ %s\n' "${FILE_LIST[@]##*/}"`"
  77.  
  78.         # Construct the name of the merged file.
  79.         OUTPUT_FILE="${VIDEO_FILE_NAME}.sub.mkv"
  80.  
  81.         # Merge the files
  82.         mkvmerge-video-and-sub-files       
  83.  
  84.         # Move video and subtitle files into user's trash directory and create trash infofile
  85.         if [ -f "$OUTPUT_FILE" ]
  86.         then
  87.             gvfs-trash "$VIDEO_FULL_FILE_NAME"
  88.             gvfs-trash "$SUB_FULL_FILE_NAME"           
  89.             notify-send "OK" "`echo -e "THE NAME OF THE NEW MERGED FILE IS:\n${OUTPUT_FILE##*/}"`"
  90.         else
  91.             notify-send "ERROR 1" "`echo "Something went wrong!"`"
  92.         fi
  93.     else
  94.         notify-send "ERROR" "`echo -e "\n\t\nTo use this function, please select exactly:\n\t- 1 video file and\n\t- 1 subtitle file!\n\t\nYou are selected these files:"; printf '\t-\ %s\n' "${FILE_LIST[@]##*/}"`"
  95.     fi
  96.  
  97. else # Scenario 2
  98.  
  99.     # Get the current directory name
  100.     DIR_NAME="${PWD##*/}"
  101.  
  102.     # Create Backup sub-directory
  103.     BACKUP_DIR="${DIR_NAME}.backup"
  104.     [ -d "${BACKUP_DIR}" ] || mkdir "$BACKUP_DIR" && echo "The directory $BACKUP_DIR was CREATED.\n" > "$MERGE_LOG"
  105.  
  106.     # Move all sub-directories into the Backup directory
  107.     shopt -s dotglob
  108.     find ./* -maxdepth 0 -type d ! -name "*$BACKUP_DIR*" -prune -exec mv "{}" "$BACKUP_DIR" \; -exec echo "The directory {} was REMOVED.\n" >> "$MERGE_LOG" \;
  109.     shopt -u dotglob
  110.  
  111.     # Move all files and folders, whose names contains a string, that exists in $FILTER[@]
  112.     for f in $FILTER; do
  113.         shopt -s dotglob
  114.         find ./* -maxdepth 0 ! -name "*$BACKUP_DIR*" -type f -name "*$f*" -exec mv "{}" "$BACKUP_DIR" \; -exec echo "The file {} was REMOVED.\n" >> "$MERGE_LOG" \;
  115.         shopt -u dotglob
  116.     done
  117.  
  118.     # Get the entire content of the current directory
  119.     get-the-content-of-the-current-directory
  120.  
  121.     # Get the names of the video and subtitle files and move the rest of the files into the Backup directory
  122.     get-video-and-sub-file-names
  123.  
  124.     # Construct the name of the merged file. It could be based on the parent directory or on the video file name Make your choice and comment/uncomment next lines
  125.     #OUTPUT_FILE="${VIDEO_FILE_NAME}.sub.mkv"
  126.     OUTPUT_FILE="${DIR_NAME}.sub.mkv"
  127.  
  128.     # Get the entire content of the current directory after the filtering
  129.     get-the-content-of-the-current-directory
  130.  
  131.     echo -e "$(cat $MERGE_LOG)" && notify-send "OK" "`echo -e "$(cat $MERGE_LOG)"`" && echo > "$MERGE_LOG"
  132.  
  133.     # Check the current structure of the directory
  134.     if [ "${#FILE_LIST[@]}" -ne "3" ]; then
  135.         echo "The content structure must consists of next 3 items:" > "$MERGE_LOG"
  136.         echo "\t- 1 movie file,\n\t- 1 subtitle file and\n\t- 1 backup directory." >> "$MERGE_LOG"
  137.         echo "\n\t\nThe current number of contained items is ${#FILE_LIST[@]}." >> "$MERGE_LOG" && echo >> "$MERGE_LOG"
  138.         echo "\n\t\nPLEASE RESOLVE THIS MANUALLY!" >> "$MERGE_LOG"
  139.         echo -e "$(cat $MERGE_LOG)" && notify-send "ERROR" "`echo -e "$(cat $MERGE_LOG)"`"
  140.     else
  141.         echo "The directory structure looks good, is contains ${#FILE_LIST[@]} items." > "$MERGE_LOG"
  142.         echo " - The source VIDEO file is: ${VIDEO_FULL_FILE_NAME::21}... .${VIDEO_FULL_FILE_NAME##*.}" >> "$MERGE_LOG"
  143.         echo " - The source SUB file is: ${SUB_FULL_FILE_NAME::25}... .${SUB_FULL_FILE_NAME##*.}" >> "$MERGE_LOG"
  144.         echo "They has been merged and removed!" | tr /a-z/ /A-Z/ >> "$MERGE_LOG"
  145.  
  146.         # Merge the files
  147.         mkvmerge-video-and-sub-files
  148.  
  149.         # Move video and subtitle files into the Backup directory
  150.         mv "$VIDEO_FULL_FILE_NAME" "$BACKUP_DIR"
  151.         mv "$SUB_FULL_FILE_NAME" "$BACKUP_DIR"
  152.  
  153.         # Move the Backup directory to trash and create trash infofile
  154.         if [ -f "$OUTPUT_FILE" ]; then
  155.             gvfs-trash "$BACKUP_DIR"
  156.  
  157.             echo "\n\t\nThe Backup directory has been MOVED to Trash!\n\t\n" >> "$MERGE_LOG"
  158.             echo "The name of the new merged file is:"  | tr /a-z/ /A-Z/ >> "$MERGE_LOG"
  159.             echo "${OUTPUT_FILE##*/}" >> "$MERGE_LOG"
  160.             echo -e "$(cat $MERGE_LOG)" && notify-send "OK" "`echo -e "$(cat $MERGE_LOG)"`"
  161.         else
  162.             echo "Something went wrong!" && notify-send "ERROR 2" "`echo "Something went wrong!"`"
  163.         fi
  164.  
  165.     fi
  166. fi
  167.  
  168. rm "$MERGE_LOG"
  169. exit 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement