Advertisement
cipherhawk

get-youtube-playlist.sh

Jan 11th, 2025
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Function to display usage information
  4. usage() {
  5.     echo "Usage: $0 [OPTIONS] <YouTube Playlist URL>"
  6.     echo "Options:"
  7.     echo "  -o, --output FILENAME    Specify custom output filename"
  8.     echo "  -f, --format FORMAT      Specify output format (default: full, options: full, simple)"
  9.     echo "  -h, --help               Display this help message"
  10.     exit 1
  11. }
  12.  
  13. # Initialize variables
  14. OUTPUT_FILE=""
  15. FORMAT="full"
  16.  
  17. # Parse command line options
  18. while [[ $# -gt 0 ]]; do
  19.     case $1 in
  20.         -o|--output)
  21.             OUTPUT_FILE="$2"
  22.             shift 2
  23.             ;;
  24.         -f|--format)
  25.             FORMAT="$2"
  26.             shift 2
  27.             ;;
  28.         -h|--help)
  29.             usage
  30.             ;;
  31.         *)
  32.             PLAYLIST_URL="$1"
  33.             shift
  34.             ;;
  35.     esac
  36. done
  37.  
  38. # Check if required tools are installed
  39. for cmd in yt-dlp jq; do
  40.     if ! command -v $cmd &> /dev/null; then
  41.         echo "$cmd could not be found. Please install it first."
  42.         exit 1
  43.     fi
  44. done
  45.  
  46. # Check if a URL was provided
  47. if [ -z "$PLAYLIST_URL" ]; then
  48.     echo "Error: Please provide a YouTube playlist URL."
  49.     usage
  50. fi
  51.  
  52. # Validate URL format
  53. if [[ ! $PLAYLIST_URL =~ ^https?://.*youtube\.com/playlist\?list= ]]; then
  54.     echo "Error: Invalid YouTube playlist URL format."
  55.     exit 1
  56. fi
  57.  
  58. # Set default output filename if not specified
  59. if [ -z "$OUTPUT_FILE" ]; then
  60.     OUTPUT_FILE="playlist_info_$(date +%Y%m%d_%H%M%S).txt"
  61. fi
  62.  
  63. echo "Fetching playlist information..."
  64.  
  65. # Prepare jq command based on format
  66. if [ "$FORMAT" = "simple" ]; then
  67.     JQ_CMD='"\(.id)\t\(.title)"'
  68. else
  69.     JQ_CMD='"https://youtu.be/\(.id)\t\(.title)\t\(.uploader)\t\(.duration_string)"'
  70. fi
  71.  
  72. # Run the yt-dlp command and save the output
  73. yt-dlp -j --verbose --flat-playlist "$PLAYLIST_URL" | \
  74. jq -r "$JQ_CMD" > "$OUTPUT_FILE"
  75.  
  76. # Check if the command was successful
  77. if [ $? -eq 0 ]; then
  78.     echo "Playlist information has been saved to $OUTPUT_FILE"
  79.     echo "Number of videos processed: $(wc -l < "$OUTPUT_FILE")"
  80. else
  81.     echo "An error occurred while processing the playlist."
  82.     exit 1
  83. fi
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement