Guest User

convert video to voice 3 && transfer files to mobile phone

a guest
Feb 9th, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.10 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Set the directory to search for files and specify the file extensions
  4. s_loc="/media/ramdisk"
  5. o_ext="mp3"  # Define output extension (e.g., mp3)
  6. log_file="$s_loc/progress.log"
  7.  
  8. # Get the current timestamp with nanoseconds
  9. timestamp() {
  10.     date +%s%N
  11. }
  12.  
  13. # Redirect all output (stdout and stderr) to the log file with append mode
  14. exec 2>> "$log_file"
  15.  
  16. # Check if detox is installed
  17. if ! command -v detox &> /dev/null; then
  18.     echo "$(timestamp): Error: Detox could not be found. Please install it before running this script." >&2
  19.     exit 1
  20. fi
  21.  
  22. # Check if ffmpeg is installed
  23. if ! command -v ffmpeg &> /dev/null; then
  24.     echo "$(timestamp): Error: Ffmpeg could not be found. Please install it before running this script." >&2
  25.     exit 1
  26. fi
  27.  
  28. # Log start message with timestamp
  29. echo "$(timestamp): Detox process started" >> "$log_file"
  30.  
  31. declare -A mp3_parent_dirs
  32.  
  33. # Search for files in s_loc with extensions matching .ts, .mp4, or .mp3
  34. while IFS= read -r sfile; do
  35.     detoxed_filename=$(echo $(detox -s iso8859_1 -v "${sfile}") | awk '/->/ {sub(/.*-> */, ""); print}')
  36.     if [ "$detoxed_filename" == "$sfile" ]; then
  37.         echo "$(timestamp): Failed to detox file: $sfile (no changes needed)" >> "$log_file"
  38.     else
  39.         echo "$(timestamp): Successfully detoxed file: $sfile to $detoxed_filename" >> "$log_file"
  40.     fi
  41.  
  42.     # Get the extension of the file
  43.     ext="${sfile##*.}"
  44.  
  45.     if [ "$ext" == "mp3" ]; then
  46.         parent_dir=$(dirname "$sfile")
  47.         mp3_parent_dirs["$parent_dir"]=1
  48.         # Move mp3 files back to s_loc with their original name
  49.         mv "$sfile" "$s_loc/$(basename "$sfile")"
  50.         if [ $? -ne 0 ]; then
  51.             echo "$(timestamp): Error: Failed to move file: $sfile to $s_loc/$(basename "$sfile") (mv command failed)" >&2
  52.         fi
  53.     else
  54.         # Check if detoxed_filename is not an empty string and file does not exist
  55.         if [ ! -z "$detoxed_filename" ] && [ ! -f "$detoxed_filename" ]; then
  56.             echo "$(timestamp): Error: File does not exist or is not a file: $detoxed_filename" >&2
  57.             exit 1
  58.         fi
  59.  
  60.         # Generate new filename with timestamp and first 50 characters of the old file name
  61.         filename=$(basename "$detoxed_filename")
  62.         base="${filename%.*}"
  63.         new_filename="$(timestamp)_${base:0:50}.${ext}"
  64.         new_sfile="$s_loc/$new_filename"
  65.  
  66.         # Move the detoxed file to the new filename
  67.         mv "$detoxed_filename" "$new_sfile"
  68.         if [ $? -ne 0 ]; then
  69.             echo "$(timestamp): Error: Failed to move file: $detoxed_filename to $new_sfile (mv command failed)" >&2
  70.         fi
  71.     fi
  72.  
  73.     # Delete the empty parent directory
  74.     parent_dir=$(dirname "$sfile")
  75.     if [ -n "$parent_dir" ] && [ "$parent_dir" != "/" ]; then
  76.         rmdir --parents --ignore-fail-on-non-empty "$parent_dir"
  77.         if [ $? -ne 0 ]; then
  78.             echo "$(timestamp): Error: Failed to delete empty parent directory: $parent_dir (rmdir command failed)" >&2
  79.         fi
  80.     fi
  81. done < <(find "$s_loc" \( -iname "*.ts" -o -iname "*.mp4" -o -iname "*.mp3" \) -type f | sort -u)
  82. if [ $? -ne 0 ]; then
  83.     echo "$(timestamp): Error: Failed to find files in $s_loc (find command failed)" >&2
  84. fi
  85.  
  86. # Log completion message with timestamp
  87. echo "$(timestamp): Detox process completed" >> "$log_file"
  88.  
  89. # Additional section to force delete parent directories, regardless of their contents
  90. for parent_dir in "${!mp3_parent_dirs[@]}"; do
  91.     echo "$(timestamp): Forcing deletion of non-empty parent directory: $parent_dir" >> "$log_file"
  92.     rm -rf "$parent_dir"
  93.     if [ $? -ne 0 ]; then
  94.         echo "$(timestamp): Error: Failed to delete directory '$parent_dir'" >&2
  95.     fi
  96. done
  97.  
  98. # Search for ts and mp4 files in s_loc and apply the ffmpeg command
  99. while IFS= read -r input_file; do
  100.     if [ -f "$input_file" ]; then
  101.         sfile=$(basename "$input_file")
  102.         sbase="${sfile%.*}"
  103.         output_filename="${sbase}.${o_ext}"
  104.         output_file="$s_loc/$output_filename"
  105.         cmd="ffmpeg -nostdin -i '$input_file' -vn -acodec libmp3lame -ac 2 -ab 32k -ar 16000 '$output_file'"
  106.         echo "$(timestamp): Applying ffmpeg command to $input_file: $cmd" >> "$log_file"
  107.        
  108.         # Execute the command and capture its exit status
  109.         eval "$cmd" && status=$? || status=$?
  110.        
  111.         if [ $status -eq 0 ]; then
  112.             echo "$(timestamp): Successfully converted $input_file to $output_file" >> "$log_file"
  113.         else
  114.             echo "$(timestamp): Error: Converting $input_file. Exit code: $status" >&2
  115.         fi
  116.     fi
  117. done < <(find "$s_loc" \( -iname "*.ts" -o -iname "*.mp4" \) -type f)
  118. if [ $? -ne 0 ]; then
  119.     echo "$(timestamp): Error: Failed to find files in $s_loc (find command failed)" >&2
  120. fi
  121.  
  122. # Log completion of ffmpeg processing
  123. echo "$(timestamp): FFmpeg processing completed" >> "$log_file"
  124.  
  125. # Organize files by current date and file extension
  126. d_loc=$(echo $s_loc/$(date '+%Y-%m-%d_%H-%M-%S'))
  127. while IFS= read -r sfile; do
  128.     if [ -f "$sfile" ]; then
  129.  
  130.         # Get the extension of the file
  131.         ext="${sfile##*.}"
  132.  
  133.         if [ "$ext" == "mp3" ]; then
  134.             output_directory="$d_loc/$ext"
  135.         else
  136.             output_directory="$d_loc/video"
  137.         fi
  138.  
  139.         # Create output_directory structure
  140.         if [ ! -d "$output_directory" ]; then
  141.             mkdir -p "$output_directory"
  142.             # Check if the mkdir command was successful
  143.             if [ $? -ne 0 ]; then
  144.                 echo "$(timestamp): Error: Failed to create folder: $output_directory exiting..." >&2
  145.                 return 1
  146.             fi
  147.         fi
  148.         fname=$(basename "$sfile")
  149.         mv "$sfile" "$output_directory/$fname"
  150.         if [ $? -ne 0 ]; then
  151.             echo "$(timestamp): Error: Failed to move file: $sfile to $output_directory/$fname exiting..." >&2
  152.             return 1
  153.         fi
  154.     fi
  155. done < <(find "$s_loc" -maxdepth 1 \( -iname "*.ts" -o -iname "*.mp4" -o -iname "*.mp3" \) -type f | sort -u)
  156.  
  157. # Log completion of organize files by current date and file extension
  158. echo "$(timestamp): Organize files by current date and file extension completed" >> "$log_file"
  159.  
  160. # copy folders to droid device
  161. gvfs_loc="/run/user/1000/gvfs/"
  162. sd_subd="/android/Movies/"
  163. droid_mpoint=$(ls "$gvfs_loc" | grep "Redmi")
  164. out_droid_dir=$gvfs_loc$droid_mpoint$sd_subd
  165.  
  166. if [ -d "$out_droid_dir" ]; then
  167.     c_year=$(date '+%Y-')
  168.    
  169.     find "$s_loc" -maxdepth 1 -type d -name "$c_year*" -print0 | while IFS= read -r -d '' bdir; do
  170.         if [ -d "$bdir" ]; then
  171.             mv "$bdir" "$out_droid_dir"
  172.             if [ $? -ne 0 ]; then
  173.                 echo "$(timestamp): Error: Moving directory $bdir to $out_droid_dir" >&2
  174.             fi
  175.         fi
  176.     done
  177.    
  178.     xdg-open "$out_droid_dir" > /dev/null 2>&1
  179.     if [ $? -ne 0 ]; then
  180.         echo "$(timestamp): Error: Opening directory $out_droid_dir" >&2
  181.     fi
  182.  
  183. else
  184.     echo "$(timestamp): Error: Output directory $out_droid_dir does not exist" >&2
  185. fi
  186.  
  187. # Log completion of copy folders to droid device
  188. echo "$(timestamp): Copy folders to droid device completed" >> "$log_file"
Advertisement
Add Comment
Please, Sign In to add comment