Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Function to display usage information
- usage() {
- echo "Usage: $0 [OPTIONS] <YouTube Playlist URL>"
- echo "Options:"
- echo " -o, --output FILENAME Specify custom output filename"
- echo " -f, --format FORMAT Specify output format (default: full, options: full, simple)"
- echo " -h, --help Display this help message"
- exit 1
- }
- # Initialize variables
- OUTPUT_FILE=""
- FORMAT="full"
- # Parse command line options
- while [[ $# -gt 0 ]]; do
- case $1 in
- -o|--output)
- OUTPUT_FILE="$2"
- shift 2
- ;;
- -f|--format)
- FORMAT="$2"
- shift 2
- ;;
- -h|--help)
- usage
- ;;
- *)
- PLAYLIST_URL="$1"
- shift
- ;;
- esac
- done
- # Check if required tools are installed
- for cmd in yt-dlp jq; do
- if ! command -v $cmd &> /dev/null; then
- echo "$cmd could not be found. Please install it first."
- exit 1
- fi
- done
- # Check if a URL was provided
- if [ -z "$PLAYLIST_URL" ]; then
- echo "Error: Please provide a YouTube playlist URL."
- usage
- fi
- # Validate URL format
- if [[ ! $PLAYLIST_URL =~ ^https?://.*youtube\.com/playlist\?list= ]]; then
- echo "Error: Invalid YouTube playlist URL format."
- exit 1
- fi
- # Set default output filename if not specified
- if [ -z "$OUTPUT_FILE" ]; then
- OUTPUT_FILE="playlist_info_$(date +%Y%m%d_%H%M%S).txt"
- fi
- echo "Fetching playlist information..."
- # Prepare jq command based on format
- if [ "$FORMAT" = "simple" ]; then
- JQ_CMD='"\(.id)\t\(.title)"'
- else
- JQ_CMD='"https://youtu.be/\(.id)\t\(.title)\t\(.uploader)\t\(.duration_string)"'
- fi
- # Run the yt-dlp command and save the output
- yt-dlp -j --verbose --flat-playlist "$PLAYLIST_URL" | \
- jq -r "$JQ_CMD" > "$OUTPUT_FILE"
- # Check if the command was successful
- if [ $? -eq 0 ]; then
- echo "Playlist information has been saved to $OUTPUT_FILE"
- echo "Number of videos processed: $(wc -l < "$OUTPUT_FILE")"
- else
- echo "An error occurred while processing the playlist."
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement