Advertisement
subbass

dac.sh - grabber v3

Apr 14th, 2024 (edited)
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.00 KB | Software | 0 0
  1. #!/bin/bash
  2. # dac.sh - Download And Convert script (Grabber 3.0)
  3. # This script downloads videos from a provided URL and converts them to the x265 (HEVC) format using yt-dlp and ffmpeg.
  4. # It supports input directly as a command-line argument, from the clipboard, or from the current text selection.
  5. # The script allows specifying the output filename or directory for the converted video using the -o flag.
  6. #
  7. # Version: 3.0
  8. # Date: 2024-04-07
  9. # Author: Subbass
  10. # Requirements: yt-dlp, ffmpeg, xclip
  11. # Usage:
  12. #   ./dac.sh [URL] - Downloads and converts the video at [URL], saving it with the original filename.
  13. #   ./dac.sh [URL] -o <output_file> - Downloads and converts the video at [URL], saving it as <output_file>.
  14. #   ./dac.sh - If no URL is provided, the script checks the clipboard and primary selection for a valid URL.
  15. #
  16. # Changelog:
  17. #   v1.0 - Initial release.
  18. #   v1.1 - Added conversion to x265, replacing the original file.
  19. #   v1.2 - Added support for checking both clipboard and primary selection for URLs.
  20. #   v1.3 - Refined script documentation and added versioning and date information.
  21. #   v2.0 - Added support for specifying output filename/directory using -o flag.
  22. #         - Renamed files using yt-dlp -o option and passed it to ffmpeg for consistency.
  23. #   v2.1 - Corrected handling of -o flag and parameters.
  24. #   v2.2 - Fix file name handling
  25. #   v2.21- Fix "noiseyness" of output
  26. #   v3.0 - Integrated error handling for yt-dlp with long filenames and updated temporary directory handling for performance.
  27.  
  28. download_and_convert() {
  29.   local url="$1"
  30.   local output_file="$2"
  31.   echo "Downloading from: $url"
  32.  
  33.   # Define and use a temporary directory for the download and conversion process.
  34.   local temp_dir=$(mktemp -d "~/temp_video_download_XXXXXX")
  35.   echo "Temp folder at: $temp_dir"
  36.   pushd "$temp_dir" > /dev/null
  37.  
  38.   # Attempt to download the video file, handling long filename error.
  39.   if [[ -z "$output_file" ]]; then
  40.     yt-dlp --quiet --progress -w "$url" -o "%(title)s.%(ext)s"
  41.   else
  42.     yt-dlp --quiet --progress -w "$url" -o "${output_file}.%(ext)s"
  43.   fi
  44.  
  45.   if [ $? -ne 0 ]; then
  46.     echo "Download failed, possibly due to a long filename."
  47.     echo "Please enter a new filename (without extension):"
  48.     read -r new_filename
  49.     if [[ -z "$output_file" ]]; then
  50.       yt-dlp --quiet --progress -w "$url" -o "${new_filename}.%(ext)s"
  51.     else
  52.       yt-dlp --quiet --progress -w "$url" -o "${new_filename}.%(ext)s"
  53.       output_file="${new_filename}"
  54.     fi
  55.   fi
  56.  
  57.   local downloaded_file=$(find . -type f -printf "%f\n" | head -n 1)
  58.  
  59.   if [[ ! -f "$downloaded_file" ]]; then
  60.     echo "Download failed or file not found: $downloaded_file"
  61.     popd > /dev/null
  62.     rm -rf "$temp_dir"
  63.     return
  64.   fi
  65.  
  66.   echo "Downloaded file: $downloaded_file"
  67.  
  68.   # Proceed with conversion if the file exists.
  69.   echo "Converting to x265..."
  70.   local new_file="${downloaded_file%.*}_x265.mp4"
  71.   ffmpeg -hide_banner -i "$downloaded_file" -c:v libx265 -preset fast -x265-params crf=28 "$new_file" -loglevel warning -stats
  72.  
  73.   if [ -f "$new_file" ]; then
  74.     # Move the final file to the original directory.
  75.     mv "$new_file" ..
  76.     echo "Conversion complete: $new_file"
  77.   else
  78.     echo "Conversion failed."
  79.   fi
  80.  
  81.   # Cleanup
  82.   popd > /dev/null
  83.   rm -rf "$temp_dir"
  84. }
  85.  
  86.  
  87. get_url_from_clipboard_or_selection() {
  88.   local url=$(xclip -o 2>/dev/null)
  89.   if [[ $url =~ ^https?:// ]]; then
  90.     echo $url
  91.     return
  92.   fi
  93.  
  94.   url=$(xclip -o -selection primary 2>/dev/null)
  95.   if [[ $url =~ ^https?:// ]]; then
  96.     echo $url
  97.     return
  98.   fi
  99.  
  100.   echo ""
  101. }
  102.  
  103. if [ $# -eq 0 ]; then
  104.   url=$(get_url_from_clipboard_or_selection)
  105.   if [ -n "$url" ]; then
  106.     download_and_convert "$url"
  107.   else
  108.     echo "No valid URL supplied, found in clipboard, or selected."
  109.   fi
  110. elif [[ "$1" == "-o" ]]; then
  111.   if [[ $# -ne 3 ]]; then
  112.     echo "Invalid usage of -o flag. Please provide a URL and a filename after -o."
  113.     exit 1
  114.   fi
  115.   download_and_convert "$3" "$2"
  116. else
  117.   download_and_convert "$1"
  118. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement