Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- CURRENT_VERSION="3.82"
- # Download And Convert Script
- # Description:
- # 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 and can automatically open the file after conversion.
- # Dependencies:
- # - yt-dlp: YouTube downloader
- # - ffmpeg: Video converter
- # - xclip: Clipboard utility
- # - xdg-utils: For opening files
- # - curl: For checking updates
- #
- # To install dependencies on Manjaro (Arch), run:
- # sudo pacman -S yt-dlp ffmpeg xclip xdg-utils curl
- # Features:
- # - Download videos from various platforms supported by yt-dlp
- # - Convert videos to x265 (HEVC) format for better compression
- # - Specify custom output filenames
- # - Automatically open converted files
- # - Use URLs from clipboard or text selection if not provided as an argument
- # - Support for Jellyfin FFmpeg installation
- # - Option to keep the original downloaded file
- # Usage:
- # ./dac.sh [OPTIONS] [URL]
- #
- # Options:
- # -o <filename> Specify output filename
- # -e Open file after conversion
- # -c <preference> Set clipboard preference (clipboard|primary|both)
- # -k, --keep-original Keep the original downloaded file
- # -h, --help Display this help message
- # Examples:
- # 1. Download and convert a video:
- # ./dac.sh https://www.youtube.com/watch?v=dQw4w9WgXcQ
- #
- # 2. Download, convert, and specify output filename:
- # ./dac.sh -o my_video https://www.youtube.com/watch?v=dQw4w9WgXcQ
- #
- # 3. Download, convert, and open after conversion:
- # ./dac.sh -e https://www.youtube.com/watch?v=dQw4w9WgXcQ
- #
- # 4. Download, convert, and keep the original file:
- # ./dac.sh -k https://www.youtube.com/watch?v=dQw4w9WgXcQ
- #
- # 5. Use without URL (will check clipboard and selection):
- # ./dac.sh
- # Changelog:
- # v3.9 - Fixed temporary folder removal issue, added option to keep original file
- # v3.8 - Improved clipboard/primary selection handling, added user-specified preference
- # v3.7 - Improved error handling, portability, and overall script structure
- # ... (previous changelog entries)
- UPDATE_URL="https://snippet.host/hghsch/raw"
- EDIT_URL="https://snippet.host/hghsch"
- check_for_updates() {
- echo "Checking for updates..."
- if ! command -v curl &> /dev/null; then
- echo "curl is required for update checks but is not installed. Skipping update check."
- return
- fi
- local latest_version=$(curl -s "$UPDATE_URL" | grep -oP '(?<=CURRENT_VERSION=")[0-9.]+')
- if [[ -z "$latest_version" ]]; then
- echo "Failed to fetch the latest version. Skipping update check."
- return
- fi
- if [[ "$(printf '%s\n' "$CURRENT_VERSION" "$latest_version" | sort -V | tail -n1)" != "$CURRENT_VERSION" ]]; then
- echo "New version ($latest_version) is available at $EDIT_URL."
- echo "You are currently using version $CURRENT_VERSION."
- elif [[ "$CURRENT_VERSION" != "$latest_version" ]]; then
- echo "Local version ($CURRENT_VERSION) is higher than online version ($latest_version)."
- echo "Online version at: $EDIT_URL"
- else
- echo "Your script is up to date (Version $CURRENT_VERSION)."
- fi
- }
- preprocess_url() {
- local url="$1"
- # Convert x.com to twitter.com
- url="${url//x.com/twitter.com}"
- # Add more URL preprocessing rules here if needed
- echo "$url"
- }
- get_ffmpeg_path() {
- if [ -f "/usr/lib/jellyfin-ffmpeg/ffmpeg" ]; then
- echo "/usr/lib/jellyfin-ffmpeg/ffmpeg"
- else
- echo "ffmpeg"
- fi
- }
- download_video() {
- local url="$1"
- local output_format="$2"
- local temp_dir="$3"
- pushd "$temp_dir" > /dev/null
- yt-dlp --quiet --progress -w "$url" -o "$output_format"
- local exit_status=$?
- popd > /dev/null
- return $exit_status
- }
- convert_video() {
- local input_file="$1"
- local output_file="$2"
- local ffmpeg_path="$3"
- echo "Converting to x265 using $ffmpeg_path..."
- "$ffmpeg_path" -hide_banner -i "$input_file" -c:v libx265 -preset fast -x265-params crf=28 "$output_file" -loglevel warning -stats
- return $?
- }
- get_url_from_clipboard_or_selection() {
- local preference="${1:-both}"
- local clipboard_content=$(xclip -o -selection clipboard 2>/dev/null)
- local primary_content=$(xclip -o -selection primary 2>/dev/null)
- case "$preference" in
- clipboard)
- [[ "$clipboard_content" =~ ^https?:// ]] && echo "$clipboard_content" || echo ""
- ;;
- primary)
- [[ "$primary_content" =~ ^https?:// ]] && echo "$primary_content" || echo ""
- ;;
- both)
- if [[ "$primary_content" =~ ^https?:// ]]; then
- echo "$primary_content"
- elif [[ "$clipboard_content" =~ ^https?:// ]]; then
- echo "$clipboard_content"
- else
- echo ""
- fi
- ;;
- *)
- echo ""
- ;;
- esac
- }
- cleanup() {
- if [ -d "$temp_dir" ] && [ "$keep_original" != "true" ]; then
- rm -rf "$temp_dir"
- echo "Temporary directory removed: $temp_dir"
- elif [ "$keep_original" == "true" ]; then
- echo "Original file kept in: $temp_dir"
- fi
- }
- main() {
- check_for_updates
- local url=""
- local output_file=""
- local open_file="false"
- local clipboard_preference="both"
- local keep_original="false"
- while [[ $# -gt 0 ]]; do
- case $1 in
- -o)
- if [[ -z "$2" || "$2" == -* ]]; then
- echo "Error: -o requires a filename argument."
- exit 1
- fi
- output_file="$2"
- shift 2
- ;;
- -e)
- open_file="true"
- shift
- ;;
- -c)
- if [[ "$2" =~ ^(clipboard|primary|both)$ ]]; then
- clipboard_preference="$2"
- shift 2
- else
- echo "Error: Invalid clipboard preference. Use 'clipboard', 'primary', or 'both'."
- exit 1
- fi
- ;;
- -k|--keep-original)
- keep_original="true"
- shift
- ;;
- -h|--help)
- echo "Usage: $(basename "$0") [OPTIONS] [URL]"
- echo "Options:"
- echo " -o <filename> Specify output filename"
- echo " -e Open file after conversion"
- echo " -c <preference> Set clipboard preference (clipboard|primary|both)"
- echo " -k, --keep-original Keep the original downloaded file"
- echo " -h, --help Display this help message"
- exit 0
- ;;
- http*://*)
- url="$1"
- shift
- ;;
- *)
- echo "Unknown option: $1"
- exit 1
- ;;
- esac
- done
- if [[ -z "$url" ]]; then
- url=$(get_url_from_clipboard_or_selection "$clipboard_preference")
- fi
- if [[ -z "$url" ]]; then
- echo "Error: No valid URL supplied, found in clipboard, or selected."
- exit 1
- fi
- url=$(preprocess_url "$url")
- echo "Processing URL: $url"
- temp_dir=$(mktemp -d)
- trap cleanup EXIT
- trap 'echo "Script interrupted. Cleaning up..."; exit 1' INT
- local yt_dlp_output
- if [[ -z "$output_file" ]]; then
- yt_dlp_output="%(title)s.%(ext)s"
- else
- yt_dlp_output="${output_file}.%(ext)s"
- fi
- if ! download_video "$url" "$yt_dlp_output" "$temp_dir"; then
- echo "Download failed. Please check the URL and try again."
- exit 1
- fi
- local downloaded_file=$(find "$temp_dir" -type f -printf "%f\n" | head -n 1)
- if [[ ! -f "$temp_dir/$downloaded_file" ]]; then
- echo "Downloaded file not found."
- exit 1
- fi
- echo "Downloaded file: $downloaded_file"
- local ffmpeg_path=$(get_ffmpeg_path)
- local new_file
- if [[ -n "$output_file" ]]; then
- new_file="${output_file}_x265.mp4"
- else
- new_file="${downloaded_file%.*}_x265.mp4"
- fi
- if ! convert_video "$temp_dir/$downloaded_file" "$temp_dir/$new_file" "$ffmpeg_path"; then
- echo "Conversion failed. Keeping original downloaded file."
- mv "$temp_dir/$downloaded_file" "./$downloaded_file"
- echo "Original downloaded file kept at: $PWD/$downloaded_file"
- exit 1
- fi
- mv "$temp_dir/$new_file" "./$new_file"
- echo "Conversion complete: $PWD/$new_file"
- if [[ "$keep_original" == "true" ]]; then
- mv "$temp_dir/$downloaded_file" "./$downloaded_file"
- echo "Original file kept at: $PWD/$downloaded_file"
- fi
- if [[ "$open_file" == "true" ]]; then
- echo "Opening file: $PWD/$new_file"
- xdg-open "$PWD/$new_file" &>/dev/null &
- fi
- }
- main "$@"
Advertisement
Add Comment
Please, Sign In to add comment