Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Set the directory to search for files and specify the file extensions
- s_loc="/media/ramdisk"
- o_ext="mp3" # Define output extension (e.g., mp3)
- log_file="$s_loc/progress.log"
- # Get the current timestamp with nanoseconds
- timestamp() {
- date +%s%N
- }
- # Redirect all output (stdout and stderr) to the log file with append mode
- exec 2>> "$log_file"
- # Check if detox is installed
- if ! command -v detox &> /dev/null; then
- echo "$(timestamp): Error: Detox could not be found. Please install it before running this script." >&2
- exit 1
- fi
- # Check if ffmpeg is installed
- if ! command -v ffmpeg &> /dev/null; then
- echo "$(timestamp): Error: Ffmpeg could not be found. Please install it before running this script." >&2
- exit 1
- fi
- # Log start message with timestamp
- echo "$(timestamp): Detox process started" >> "$log_file"
- declare -A mp3_parent_dirs
- # Search for files in s_loc with extensions matching .ts, .mp4, or .mp3
- while IFS= read -r sfile; do
- detoxed_filename=$(echo $(detox -s iso8859_1 -v "${sfile}") | awk '/->/ {sub(/.*-> */, ""); print}')
- if [ "$detoxed_filename" == "$sfile" ]; then
- echo "$(timestamp): Failed to detox file: $sfile (no changes needed)" >> "$log_file"
- else
- echo "$(timestamp): Successfully detoxed file: $sfile to $detoxed_filename" >> "$log_file"
- fi
- # Get the extension of the file
- ext="${sfile##*.}"
- if [ "$ext" == "mp3" ]; then
- parent_dir=$(dirname "$sfile")
- mp3_parent_dirs["$parent_dir"]=1
- # Move mp3 files back to s_loc with their original name
- mv "$sfile" "$s_loc/$(basename "$sfile")"
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to move file: $sfile to $s_loc/$(basename "$sfile") (mv command failed)" >&2
- fi
- else
- # Check if detoxed_filename is not an empty string and file does not exist
- if [ ! -z "$detoxed_filename" ] && [ ! -f "$detoxed_filename" ]; then
- echo "$(timestamp): Error: File does not exist or is not a file: $detoxed_filename" >&2
- exit 1
- fi
- # Generate new filename with timestamp and first 50 characters of the old file name
- filename=$(basename "$detoxed_filename")
- base="${filename%.*}"
- new_filename="$(timestamp)_${base:0:50}.${ext}"
- new_sfile="$s_loc/$new_filename"
- # Move the detoxed file to the new filename
- mv "$detoxed_filename" "$new_sfile"
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to move file: $detoxed_filename to $new_sfile (mv command failed)" >&2
- fi
- fi
- # Delete the empty parent directory
- parent_dir=$(dirname "$sfile")
- if [ -n "$parent_dir" ] && [ "$parent_dir" != "/" ]; then
- rmdir --parents --ignore-fail-on-non-empty "$parent_dir"
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to delete empty parent directory: $parent_dir (rmdir command failed)" >&2
- fi
- fi
- done < <(find "$s_loc" \( -iname "*.ts" -o -iname "*.mp4" -o -iname "*.mp3" \) -type f | sort -u)
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to find files in $s_loc (find command failed)" >&2
- fi
- # Log completion message with timestamp
- echo "$(timestamp): Detox process completed" >> "$log_file"
- # Additional section to force delete parent directories, regardless of their contents
- for parent_dir in "${!mp3_parent_dirs[@]}"; do
- echo "$(timestamp): Forcing deletion of non-empty parent directory: $parent_dir" >> "$log_file"
- rm -rf "$parent_dir"
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to delete directory '$parent_dir'" >&2
- fi
- done
- # Search for ts and mp4 files in s_loc and apply the ffmpeg command
- while IFS= read -r input_file; do
- if [ -f "$input_file" ]; then
- sfile=$(basename "$input_file")
- sbase="${sfile%.*}"
- output_filename="${sbase}.${o_ext}"
- output_file="$s_loc/$output_filename"
- cmd="ffmpeg -nostdin -i '$input_file' -vn -acodec libmp3lame -ac 2 -ab 32k -ar 16000 '$output_file'"
- echo "$(timestamp): Applying ffmpeg command to $input_file: $cmd" >> "$log_file"
- # Execute the command and capture its exit status
- eval "$cmd" && status=$? || status=$?
- if [ $status -eq 0 ]; then
- echo "$(timestamp): Successfully converted $input_file to $output_file" >> "$log_file"
- else
- echo "$(timestamp): Error: Converting $input_file. Exit code: $status" >&2
- fi
- fi
- done < <(find "$s_loc" \( -iname "*.ts" -o -iname "*.mp4" \) -type f)
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to find files in $s_loc (find command failed)" >&2
- fi
- # Log completion of ffmpeg processing
- echo "$(timestamp): FFmpeg processing completed" >> "$log_file"
- # Organize files by current date and file extension
- d_loc=$(echo $s_loc/$(date '+%Y-%m-%d_%H-%M-%S'))
- while IFS= read -r sfile; do
- if [ -f "$sfile" ]; then
- # Get the extension of the file
- ext="${sfile##*.}"
- if [ "$ext" == "mp3" ]; then
- output_directory="$d_loc/$ext"
- else
- output_directory="$d_loc/video"
- fi
- # Create output_directory structure
- if [ ! -d "$output_directory" ]; then
- mkdir -p "$output_directory"
- # Check if the mkdir command was successful
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to create folder: $output_directory exiting..." >&2
- return 1
- fi
- fi
- fname=$(basename "$sfile")
- mv "$sfile" "$output_directory/$fname"
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Failed to move file: $sfile to $output_directory/$fname exiting..." >&2
- return 1
- fi
- fi
- done < <(find "$s_loc" -maxdepth 1 \( -iname "*.ts" -o -iname "*.mp4" -o -iname "*.mp3" \) -type f | sort -u)
- # Log completion of organize files by current date and file extension
- echo "$(timestamp): Organize files by current date and file extension completed" >> "$log_file"
- # copy folders to droid device
- gvfs_loc="/run/user/1000/gvfs/"
- sd_subd="/android/Movies/"
- droid_mpoint=$(ls "$gvfs_loc" | grep "Redmi")
- out_droid_dir=$gvfs_loc$droid_mpoint$sd_subd
- if [ -d "$out_droid_dir" ]; then
- c_year=$(date '+%Y-')
- find "$s_loc" -maxdepth 1 -type d -name "$c_year*" -print0 | while IFS= read -r -d '' bdir; do
- if [ -d "$bdir" ]; then
- mv "$bdir" "$out_droid_dir"
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Moving directory $bdir to $out_droid_dir" >&2
- fi
- fi
- done
- xdg-open "$out_droid_dir" > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "$(timestamp): Error: Opening directory $out_droid_dir" >&2
- fi
- else
- echo "$(timestamp): Error: Output directory $out_droid_dir does not exist" >&2
- fi
- # Log completion of copy folders to droid device
- echo "$(timestamp): Copy folders to droid device completed" >> "$log_file"
Advertisement
Add Comment
Please, Sign In to add comment