-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt_clip_downloader_python.py
More file actions
62 lines (45 loc) · 2.1 KB
/
yt_clip_downloader_python.py
File metadata and controls
62 lines (45 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from requests_html import HTMLSession
from bs4 import BeautifulSoup as bs
import re
import json
import os
import random
import time
# init session
def video_info(url):
session = HTMLSession() # initialise the session
time.sleep(5) #ensure there is no Future exception was never retrieved error
# download HTML code
response = session.get(url)
# execute Javascript
response.html.render(timeout=60)
# create beautiful soup object to parse HTML
soup = bs(response.html.html, "html.parser")
#Initialize the result
result = {}
#Title
result["title"] = soup.find("meta", itemprop="name")['content']
#Time Stamp
result["timeStamp"] = soup.find("div", {"class": "ytp-fine-scrubbing-seek-time"}).text
# get the duration of the video
result["duration"] = soup.find("span", {"class": "ytp-time-duration"}).text
return result
def download():
url = input("What is the url of the youtube clip? ")
res = int(input("What resolution would you like the clip downloaded in? [2160/1440/1080/720/480/360/240/144] "))
ext = input("What extension would you like the output to have? [3gp/aac/flv/m4a/mp3/mp4/ogg/wav/webm] ")
original_file_name_question = input("Would you like to keep the title of the youtube video [Y/N} ")
if original_file_name_question == "Y".upper():
keep_original_title = True
else:
file_name = input("What would you like the file name to be? ")
data = video_info(url)
duration = "00:0" + data['duration']
timeStamp = data['timeStamp']
if keep_original_title == True:
file_name = data['title']
#The download command to be sent to the command line
download_command = ('youtube-dl -f "bestvideo[height<=' + str(res) + '][ext=mp4]+bestaudio/best[height<=' + str(res) + '][ext=m4a]/' + str(ext) + '" --external-downloader ffmpeg --external-downloader-args "-ss ' + timeStamp + ' -t ' + duration + '" -o "' + str(file_name) + '" "' + url + '"')
print(download_command)
os.system(download_command)
download()