-

@ Satosha
2025-04-29 20:47:11
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check for required dependencies
check_dependencies() {
local missing_deps=()
if ! command_exists ffmpeg; then
missing_deps+=("ffmpeg")
fi
if ! command_exists yt-dlp && ! command_exists youtube-dl; then
missing_deps+=("yt-dlp or youtube-dl")
fi
if [ ${#missing_deps[@]} -ne 0 ]; then
echo "Error: The following dependencies are missing:"
for dep in "${missing_deps[@]}"; do
echo " - $dep"
done
echo "Please install them before running this script."
exit 1
fi
}
# Function to download YouTube video
download_video() {
local url="$1"
local output_dir="$2"
echo "Starting download in the background..."
if command_exists yt-dlp; then
yt-dlp -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' \
-o "$output_dir/%(title)s.%(ext)s" "$url" >/dev/null 2>&1 &
else
youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' \
-o "$output_dir/%(title)s.%(ext)s" "$url" >/dev/null 2>&1 &
fi
local download_pid=$!
echo "Download started with PID: $download_pid"
return $download_pid
}
# Function to validate timestamp format
validate_timestamp() {
local timestamp="$1"
if ! [[ $timestamp =~ ^[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
echo "Error: Invalid timestamp format. Use HH:MM:SS"
return 1
fi
return 0
}
# Function to extract clip
extract_clip() {
local input_file="$1"
local start_time="$2"
local duration="$3"
local output_file="$4"
echo "Extracting clip from $start_time for duration $duration..."
ffmpeg -i "$input_file" -ss "$start_time" -t "$duration" \
-c:v copy -c:a copy "$output_file"
if [ $? -eq 0 ]; then
echo "Successfully extracted clip to $output_file"
return 0
else
echo "Error: Failed to extract clip"
return 1
fi
}
# Main script
main() {
check_dependencies
# Create downloads directory if it doesn't exist
mkdir -p downloads
# Get YouTube URL
read -p "Enter YouTube URL: " youtube_url
# Start download in background
download_pid=$(download_video "$youtube_url" "downloads")
# Get clip details
echo -e "\nEnter the clip details:"
read -p "Start time (HH:MM:SS): " start_time
while ! validate_timestamp "$start_time"; do
read -p "Please enter a valid start time (HH:MM:SS): " start_time
done
read -p "Duration (HH:MM:SS): " duration
while ! validate_timestamp "$duration"; do
read -p "Please enter a valid duration (HH:MM:SS): " duration
done
# Wait for download to complete
echo -e "\nWaiting for download to complete..."
wait $download_pid
# Get the downloaded file
downloaded_file=$(ls -t downloads/*.mp4 | head -1)
if [ -z "$downloaded_file" ]; then
echo "Error: No video file found in downloads directory"
exit 1
fi
# Generate output filename
output_file="clips/$(basename "$downloaded_file" .mp4)_clip.mp4"
mkdir -p clips
# Ask for confirmation
echo -e "\nClip details:"
echo "Start time: $start_time"
echo "Duration: $duration"
echo "Output file: $output_file"
read -p "Do you want to proceed with extraction? (y/n): " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
extract_clip "$downloaded_file" "$start_time" "$duration" "$output_file"
else
echo "Extraction cancelled"
exit 0
fi
}
# Run main function
main