File size: 6,016 Bytes
2b5d662
155317d
2b5d662
 
7d4175a
 
 
92d4e30
5954bab
2b5d662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5954bab
 
 
2b5d662
5954bab
 
2b5d662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85c53a9
 
 
 
 
 
 
 
 
 
a21793c
23816e9
85c53a9
155317d
 
 
 
ad33eea
 
 
 
85c53a9
155317d
 
 
 
23816e9
 
a21793c
 
23816e9
85c53a9
155317d
 
 
 
ad33eea
 
 
 
85c53a9
155317d
 
 
 
23816e9
 
a21793c
 
 
 
 
 
 
 
2b5d662
 
 
92d4e30
 
2b5d662
 
92d4e30
2b5d662
92d4e30
2b5d662
a21793c
92d4e30
a21793c
92d4e30
2b5d662
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from pytube import YouTube
import yt_dlp
import os
import argparse
# import twitchdl.commands as twitch_downloader
# import twitchdl.twitch
# from twitchdl.commands.download import _parse_playlists
from tqdm import tqdm
import re

VIDEO_FOLDER = 'videos'
AUDIO_FOLDER = 'audios'

DOWNLOAD_VIDEO_NAME = 'download_video'
DOWNLOAD_AUDIO_NAME = 'download_audio'

DOWNLOAD_VIDEO_FORMAT = 'mp4'
DOWNLOAD_AUDIO_FORMAT = 'mp3'

DOWNLOAD_VIDEO = 'video'
DOWNLOAD_AUDIO = 'audio'

def download_twitch(url, type):
    # Create a new parser for the download command
    argparser = argparse.ArgumentParser(description='Download twitch video from URL')
    argparser.add_argument('--auth-token', default=None, help='Twitch auth token')
    argparser.add_argument('--chapter', default=None, help='Chapter to download')
    argparser.add_argument('--debug', default=False, help='Debug', action='store_true')
    argparser.add_argument('--end', default=None, help='End')
    argparser.add_argument('--format', default=f'{DOWNLOAD_VIDEO_FORMAT}', help='Format')
    argparser.add_argument('--keep', default=False, help='Keep', action='store_true')
    argparser.add_argument('--max_workers', default=5, help='Max workers')
    argparser.add_argument('--no_color', default=False, help='No color', action='store_true')
    argparser.add_argument('--no_join', default=False, help='No join', action='store_true')
    argparser.add_argument('--output', default=f'{VIDEO_FOLDER}/{DOWNLOAD_VIDEO_NAME}.{format}', help='Output')
    argparser.add_argument('--overwrite', default=False, help='Overwrite', action='store_true')
    argparser.add_argument('--quality', default=None, help='Quality')
    argparser.add_argument('--rate_limit', default=None, help='Rate limit')
    argparser.add_argument('--start', default=None, help='Start')
    argparser.add_argument('--version', default=False, help='Version', action='store_true')
    argparser.add_argument('videos', default=[url], help='Videos', nargs='+')
    args = argparser.parse_args()

    # Get video id
    video_id = re.search(r'(?<=videos\/)\d+', url).group(0)

    # Get qualitys
    access_token = twitchdl.twitch.get_access_token(video_id, None)
    playlists_m3u8 = twitchdl.twitch.get_playlists(video_id, access_token)
    playlists = list(_parse_playlists(playlists_m3u8))
    qualitys = [name for (name, _, _) in playlists]

    # Select quality
    if type == DOWNLOAD_VIDEO:
        args.quality = qualitys[0]
        args.format = DOWNLOAD_VIDEO_FORMAT
        args.output = f'{VIDEO_FOLDER}/{DOWNLOAD_VIDEO_NAME}.{args.format}'
    elif type == DOWNLOAD_AUDIO:
        args.quality = qualitys[-1]
        args.format = "mkv"
        args.output = f'{AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{args.format}'

    # Download
    twitch_downloader.download(args)
    if type == DOWNLOAD_AUDIO:
        os.system(f'ffmpeg -i {AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{args.format} -c:a libmp3lame -b:a 192k -stats -loglevel warning {AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{DOWNLOAD_AUDIO_FORMAT}')
        os.system(f'rm {AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{args.format}')

def create_minimal_cookies():
    cookies_content = """# Netscape HTTP Cookie File
# https://www.youtube.com/
.youtube.com	TRUE	/	FALSE	1735689600	CONSENT	YES+cb
.youtube.com	TRUE	/	FALSE	1735689600	VISITOR_INFO1_LIVE	random_string
"""
    with open('youtube_cookies.txt', 'w') as f:
        f.write(cookies_content)
    return 'youtube_cookies.txt'

def download_youtube_video(url):
    try:
        cookies_file = create_minimal_cookies()
        # Config options
        ydl_options = {
            "outtmpl": f"{VIDEO_FOLDER}/{DOWNLOAD_VIDEO_NAME}.{DOWNLOAD_VIDEO_FORMAT}",
            "format": "best",
            "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "force_ipv4": True,
            "no_check_certificates": True,
            "geo_bypass": True,
            "cookiefile": cookies_file
        }
        # Download video
        with yt_dlp.YoutubeDL(ydl_options) as ydl:
            ydl.download(url)
    except Exception as e:
        print(f'Error descargando el video: {str(e)}')

def download_youtube_audio(url):
    try:
        cookies_file = create_minimal_cookies()
        # Config options
        ydl_options = {
            "outtmpl": f"{AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{DOWNLOAD_AUDIO_FORMAT}",
            "format": "bestaudio",
            "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "force_ipv4": True,
            "no_check_certificates": True,
            "geo_bypass": True,
            "cookiefile": cookies_file
        }
        # Download audio
        with yt_dlp.YoutubeDL(ydl_options) as ydl:
            ydl.download(url)
    except Exception as e:
        print(f'Error descargando el audio: {str(e)}')

def download_youtube(url, type):
    if type == DOWNLOAD_VIDEO:
        download_youtube_video(url)
    elif type == DOWNLOAD_AUDIO:
        download_youtube_audio(url)
    else:
        print('Unknown youtube download type')

def main(args):
    url = args.url
    num_works = 2
    download_progress_bar = tqdm(total=num_works, desc='Downloading video and audio progress')
    if 'twitch' in url.lower():
        download_twitch(url, DOWNLOAD_VIDEO)
        download_progress_bar.update(1)
        download_twitch(url, DOWNLOAD_AUDIO)
        download_progress_bar.update(1)
    elif 'youtube' in url.lower() or 'youtu.be' in url.lower():
        download_youtube(url, DOWNLOAD_VIDEO)
        download_progress_bar.update(1)
        download_youtube(url, DOWNLOAD_AUDIO)
        download_progress_bar.update(1)
    else:
        print('Unknown video source')

if __name__ == "__main__":
    argparser = argparse.ArgumentParser(description='Download video from URL')
    argparser.add_argument('url', help='URL of video')
    args = argparser.parse_args()
    main(args)