Skip to content

Instantly share code, notes, and snippets.

@Macuyiko
Created January 7, 2017 18:42
Show Gist options
  • Save Macuyiko/79b1eae55a95f844f2668497862ee5fc to your computer and use it in GitHub Desktop.
Save Macuyiko/79b1eae55a95f844f2668497862ee5fc to your computer and use it in GitHub Desktop.
import youtube_dl
import sys
import os
sys.path.append('.')
if len(sys.argv) <= 1:
print("Usage: {} URL-OR-ID [OUTPUT_DIR] [SECONDS-BETWEEN]".format(__file__))
exit()
url = sys.argv[1]
output_dir = os.path.join('.', 'output') if not len(sys.argv) == 3 else sys.argv[2]
seconds_between = 10 if not len(sys.argv) == 4 else int(sys.argv[3])
if not url.startswith('http'):
url = 'http://www.youtube.com/watch?v=' + url
if not os.path.exists(output_dir):
os.makedirs(output_dir)
ydl = youtube_dl.YoutubeDL({
'ffmpeg_location': os.path.join('.', 'ffmpeg.exe'),
'format': 'bestvideo+bestaudio',
'outtmpl': os.path.join(output_dir, '%(id)s.%(ext)s')
})
with ydl:
result = ydl.extract_info(url, download=True)
video = result['entries'][0] if 'entries' in result else result
filename = os.path.join(output_dir, "{}.{}".format(video['id'], video['ext']))
if not os.path.exists(filename):
filename = os.path.join(output_dir, "{}.{}".format(video['id'], 'mkv'))
cmd = "ffmpeg -i {} -vf fps=1/{} {}".format(
filename, seconds_between,
os.path.join(output_dir, 'screenshot%04d.jpg')
)
print(cmd)
os.system(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment