Last active
February 14, 2020 08:42
-
-
Save WaxyMocha/e3cb6008f7e10208837fc8b8a09449e9 to your computer and use it in GitHub Desktop.
Download and cut youtube video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import os.path as path | |
import string | |
import random | |
import argparse | |
import subprocess | |
def randomString(stringLength=10): | |
"""Generate a random string of fixed length """ | |
letters = string.ascii_lowercase | |
return ''.join(random.choice(letters) for i in range(stringLength)) | |
parser = argparse.ArgumentParser(description='Download and cut youtube video') | |
parser.add_argument('url', type=str, help='video url') | |
parser.add_argument('-o', '--output', type=str, help='filename of saved video', required=False, default="video.mp4") | |
parser.add_argument('-s', '--start', type=str, help='start of video, HH:MM:SS', required=False) | |
parser.add_argument('-e', '--end', type=str, help='end of video, HH:MM:SS', required=False) | |
parser.add_argument('-l', '--length', type=str, help='length of video, HH:MM:SS, this option have priority over --end', required=False) | |
args = parser.parse_args() | |
filename = randomString() | |
subprocess.run(["youtube-dl", args.url, "-o", "/tmp/" + filename]) | |
for extension in ["mkv", "mp4", "webm"]: | |
if path.isfile("/tmp/" + filename + "." + extension): | |
filename += "." + extension | |
break | |
arguments = ["ffmpeg"] | |
if args.start != None: | |
arguments.extend(["-ss", args.start]) | |
if args.end != None: | |
arguments.extend(["-to", args.end]) | |
if args.length != None: | |
arguments.extend(["-t", args.length]) | |
arguments.extend(["-i", "/tmp/" + filename, "-c", "copy", "-strict", "-2", args.output]) | |
subprocess.run(arguments) | |
os.remove("/tmp/" + filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment