Skip to content

Instantly share code, notes, and snippets.

@0187773933
Created August 18, 2023 16:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0187773933/c311b341395e9f727270732d61e7e318 to your computer and use it in GitHub Desktop.
Save 0187773933/c311b341395e9f727270732d61e7e318 to your computer and use it in GitHub Desktop.
Converts YouTube Playlist ID to M3U8 - Urls Good for 6 Hours
#!/bin/bash
# Check if the input playlist ID and API key are provided
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 <playlist_id> <api_key>"
exit 1
fi
playlist_id="$1"
api_key="$2"
output_playlist="playlist.m3u8"
next_page_token=""
# Create or clear the existing M3U8 file
echo "#EXTM3U" > $output_playlist
# Loop until all pages are fetched
while true; do
# Form the URL for the curl request
url="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=$playlist_id&key=$api_key&pageToken=$next_page_token"
# Fetch the page using curl and store it in a temporary file
temp_file=$(mktemp)
curl -s "$url" > $temp_file
# Extract video IDs using jq
video_ids=$(jq -r '.items[].snippet.resourceId.videoId' $temp_file)
echo $video_ids
# Convert video IDs to URLs and get direct URLs using youtube-dl
for video_id in $video_ids; do
youtube_url="https://www.youtube.com/watch?v=$video_id"
direct_url=$(/usr/local/bin/yt-dlp --no-warnings -f best "$youtube_url" -g)
echo "$direct_url" >> $output_playlist
echo "Added: $youtube_url === $direct_url"
done
# Check if there are no more pages
next_page_token=$(jq -r '.nextPageToken // empty' $temp_file)
if [ -z "$next_page_token" ]; then
rm $temp_file
break
fi
# Remove temporary file
rm $temp_file
done
echo "Playlist created: $output_playlist"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment