Skip to content

Instantly share code, notes, and snippets.

Last active May 20, 2021 22:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ngbeslhang/47725950e93204c715b718ed8ac22ba1 to your computer and use it in GitHub Desktop.
Python 3 script that converts Youtube playlists through youtube-dl into format-agnostic VLC-compatible .m3u8 playlist file in the order of the source playlist
# Requires youtube-dl option `--write-info-json` to work
# For this to work, you need to put this script into the directory (folder) of the videos and JSON files
import os
import json
# Separate JSON metadata files and media files
for path, dirname, filenames in os.walk(os.getcwd()):
json_filenames = [name for name in filenames if name.endswith('.info.json')]
media_filenames = [name for name in filenames if not name.endswith('.info.json')]
# Creates playlist that will fit exactly all the media files in the directory/folder
playlist = [''] * len(json_filenames)
# Finds matching media filename per JSON files, then add it into the playlist file
for json_name in json_filenames:
if any(name.startswith(json_name[:-10]) for name in media_filenames) is True:
media_name = list(filter(lambda x: x.startswith(json_name[:-10]), media_filenames))[0]
info = json.loads(open(json_name).read())
playlist[info['playlist_index']-1] = media_name
# Write the playlist into at least a VLC-compatible playlist file; WARNING: WILL WRITE OVER EXISTING FILE
with open('playlist.m3u8', 'w', encoding='utf-8') as f:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment