Skip to content

Instantly share code, notes, and snippets.

@DanielOaks
Last active June 9, 2022 03:02
Show Gist options
  • Save DanielOaks/1b60ae9a0d46599800ff to your computer and use it in GitHub Desktop.
Save DanielOaks/1b60ae9a0d46599800ff to your computer and use it in GitHub Desktop.
Creates an m3u playlist from files downloaded using youtube-dl
#!/usr/bin/env python3
# creates an m3u playlist sorted by upload date and then name for videos
# downloaded using youtube-dl (with the --write-info-json option)
# written in 2015 by Daniel Oaks <[email protected]>
# released under the CC0 Public Domain license
import os
import json
from natsort import natsorted
playlist_files = []
# sort by upload date and then name
date_files = {}
for dirpath, dirnames, filenames in os.walk(os.getcwd()):
full_filenames = [name for name in filenames if name.endswith('.json')]
for name in full_filenames:
info = json.loads(open(name).read())
date = info['upload_date']
if date not in date_files:
date_files[date] = {}
date_files[date][name.replace('.info.json', '.ext')] = info
# export names
for date in sorted(date_files.keys()):
files = date_files[date]
for name in natsorted(files.keys()):
info = files[name]
for ext in ('mp4', 'mkv', 'flv'):
filename = name.replace('.ext', '.{}'.format(ext))
if os.path.exists(filename):
playlist_files.append(filename)
break
# write out playlist file
with open('playlist.m3u', 'w') as f:
f.write('\n'.join(playlist_files))
f.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment