Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # dac.sh - Download And Convert script (Grabber 3.0)
- # This script downloads videos from a provided URL and converts them to the x265 (HEVC) format using yt-dlp and ffmpeg.
- # It supports input directly as a command-line argument, from the clipboard, or from the current text selection.
- # The script allows specifying the output filename or directory for the converted video using the -o flag.
- #
- # Version: 3.0
- # Date: 2024-04-07
- # Author: Subbass
- # Requirements: yt-dlp, ffmpeg, xclip
- # Usage:
- # ./dac.sh [URL] - Downloads and converts the video at [URL], saving it with the original filename.
- # ./dac.sh [URL] -o <output_file> - Downloads and converts the video at [URL], saving it as <output_file>.
- # ./dac.sh - If no URL is provided, the script checks the clipboard and primary selection for a valid URL.
- #
- # Changelog:
- # v1.0 - Initial release.
- # v1.1 - Added conversion to x265, replacing the original file.
- # v1.2 - Added support for checking both clipboard and primary selection for URLs.
- # v1.3 - Refined script documentation and added versioning and date information.
- # v2.0 - Added support for specifying output filename/directory using -o flag.
- # - Renamed files using yt-dlp -o option and passed it to ffmpeg for consistency.
- # v2.1 - Corrected handling of -o flag and parameters.
- # v2.2 - Fix file name handling
- # v2.21- Fix "noiseyness" of output
- # v3.0 - Integrated error handling for yt-dlp with long filenames and updated temporary directory handling for performance.
- download_and_convert() {
- local url="$1"
- local output_file="$2"
- echo "Downloading from: $url"
- # Define and use a temporary directory for the download and conversion process.
- local temp_dir=$(mktemp -d "~/temp_video_download_XXXXXX")
- echo "Temp folder at: $temp_dir"
- pushd "$temp_dir" > /dev/null
- # Attempt to download the video file, handling long filename error.
- if [[ -z "$output_file" ]]; then
- yt-dlp --quiet --progress -w "$url" -o "%(title)s.%(ext)s"
- else
- yt-dlp --quiet --progress -w "$url" -o "${output_file}.%(ext)s"
- fi
- if [ $? -ne 0 ]; then
- echo "Download failed, possibly due to a long filename."
- echo "Please enter a new filename (without extension):"
- read -r new_filename
- if [[ -z "$output_file" ]]; then
- yt-dlp --quiet --progress -w "$url" -o "${new_filename}.%(ext)s"
- else
- yt-dlp --quiet --progress -w "$url" -o "${new_filename}.%(ext)s"
- output_file="${new_filename}"
- fi
- fi
- local downloaded_file=$(find . -type f -printf "%f\n" | head -n 1)
- if [[ ! -f "$downloaded_file" ]]; then
- echo "Download failed or file not found: $downloaded_file"
- popd > /dev/null
- rm -rf "$temp_dir"
- return
- fi
- echo "Downloaded file: $downloaded_file"
- # Proceed with conversion if the file exists.
- echo "Converting to x265..."
- local new_file="${downloaded_file%.*}_x265.mp4"
- ffmpeg -hide_banner -i "$downloaded_file" -c:v libx265 -preset fast -x265-params crf=28 "$new_file" -loglevel warning -stats
- if [ -f "$new_file" ]; then
- # Move the final file to the original directory.
- mv "$new_file" ..
- echo "Conversion complete: $new_file"
- else
- echo "Conversion failed."
- fi
- # Cleanup
- popd > /dev/null
- rm -rf "$temp_dir"
- }
- get_url_from_clipboard_or_selection() {
- local url=$(xclip -o 2>/dev/null)
- if [[ $url =~ ^https?:// ]]; then
- echo $url
- return
- fi
- url=$(xclip -o -selection primary 2>/dev/null)
- if [[ $url =~ ^https?:// ]]; then
- echo $url
- return
- fi
- echo ""
- }
- if [ $# -eq 0 ]; then
- url=$(get_url_from_clipboard_or_selection)
- if [ -n "$url" ]; then
- download_and_convert "$url"
- else
- echo "No valid URL supplied, found in clipboard, or selected."
- fi
- elif [[ "$1" == "-o" ]]; then
- if [[ $# -ne 3 ]]; then
- echo "Invalid usage of -o flag. Please provide a URL and a filename after -o."
- exit 1
- fi
- download_and_convert "$3" "$2"
- else
- download_and_convert "$1"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement