1+ import os
2+ import shutil
3+ import time
4+ import requests
5+
6+ from rich .console import Console
7+ from rich .markdown import Markdown
8+
9+ def main ():
10+ console = Console ()
11+
12+ console .print ()
13+ console .print (
14+ " I will install [blue]ffmpeg, opus and youtube dl (opt)[/blue] for you."
15+ )
16+ console .print (
17+ " Make sure this REPL is clear so that nothing will be overwritten."
18+ )
19+ console .print ()
20+ _next : str = console .input (
21+ " [green]?[/green] Continue? [blue](Yn)[/blue] "
22+ )
23+ console .print ()
24+
25+ if _next .lower () not in ["y" , "yes" ]:
26+ exit (1 )
27+
28+ _plain_ans_with_ytdl = console .input (
29+ " [green]?[/green] Would you also like me to install "
30+ "[red]youtube dl[/red]? [blue](Yn)[/blue] "
31+ )
32+ console .print ()
33+
34+ with_ytdl : bool = _plain_ans_with_ytdl .lower () in ["y" , "yes" ]
35+
36+ r = None
37+
38+ headers = {
39+ "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 OPR/100.0.0.0 (Edition GX-CN)"
40+ }
41+
42+ def download_ffmpeg ():
43+ global r
44+ r = requests .get ("https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz" , headers = headers )
45+
46+ console .print (
47+ "[blue]install[/blue] ffmpeg static "
48+ "[d white](2x attempts)[/d white]"
49+ )
50+
51+ START = time .time ()
52+
53+ for _ in range (2 ):
54+ try :
55+ download_ffmpeg ()
56+ break
57+ except requests .exceptions .SSLError :
58+ console .print (
59+ "[d red]error [/d red][d] SSL error: retrying[/d]"
60+ )
61+
62+ console .print (
63+ "[blue]unpack [/blue] ffmpeg static "
64+ "[d white]@ ffmpeg.tar.xz[/d white]"
65+ )
66+ with open ('ffmpeg.tar.xz' , 'wb' ) as f :
67+ f .write (r .content )
68+
69+ os .system ('tar -xf ffmpeg.tar.xz' )
70+
71+ console .print (
72+ "[blue]moving [/blue] ffmpeg, ffprobe"
73+ )
74+ os .system ('mv ffmpeg-6.0-amd64-static/ffmpeg ffmpeg' )
75+ os .system ('mv ffmpeg-6.0-amd64-static/ffprobe ffprobe' )
76+
77+ console .print ("[red]remove [/red] ffmpeg.tar.xz" )
78+ os .remove ('ffmpeg.tar.xz' )
79+
80+ # https://www.linuxfromscratch.org/blfs/view/svn/multimedia/opus.html
81+
82+ console .print (
83+ "[blue]install[/blue] opus archive"
84+ )
85+
86+ opus_r = requests .get ("https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz" , headers = headers )
87+
88+ with open ("opus-1.3.1.tar.gz" , "wb" ) as f :
89+ f .write (opus_r .content )
90+
91+ console .print (
92+ "[blue]unpack [/blue] opus archive "
93+ "[d white]@ opus-1.3.1.tar.gz[/d white]"
94+ )
95+
96+ os .system ('tar -xf opus-1.3.1.tar.gz' )
97+ os .remove ("opus-1.3.1.tar.gz" )
98+
99+ console .print (
100+ "[blue]command[/blue] 'configure' & 'make'"
101+ )
102+ console .print ()
103+
104+ os .system ("""cd opus-1.3.1 && ./configure --prefix=/usr \
105+ --disable-static \
106+ --docdir=/usr/share/doc/opus-1.3.1 && make""" )
107+
108+ console .print ()
109+ console .print (
110+ "[green]success[/green] built opus"
111+ )
112+ os .system ("""mkdir opus && mv opus-1.3.1/.libs/* opus/""" )
113+ console .print (
114+ "[blue]moving [/blue] 'libopus.so', 'libopus.so.0', and many other."
115+ )
116+
117+ console .print ()
118+
119+ console .print (Markdown (
120+ """
121+ # It's DONE!
122+ Congratulations! We've installed ffmpeg and opus for your REPL!
123+
124+ In constrast, I've:
125+
126+ - Installed `ffmpeg` and `ffprobe` in this directory
127+ - Installed `opus`
128+ - Created a dir named `opus` that contains `libopus.so` files
129+
130+ ...that's pretty much it!
131+
132+ If you're making a Discord bot, whether it's `py-cord` or `discord.py`, use:
133+
134+ ```python
135+ import discord
136+
137+ discord.opus.load_opus("opus/libopus.so")
138+ ```
139+
140+ That's it, and have fun hacking! <3
141+ """ ,
142+ "one-dark"
143+ ))
144+
145+ console .print ()
146+
147+ if with_ytdl :
148+ console .print (
149+ "[d blue]extra [/d blue][d] downloading yt-dlp[/d]..."
150+ )
151+ os .system ('pip install yt-dlp --quiet' )
152+ console .print (
153+ "[d blue]extra [/d blue][d] installed yt-dlp. "
154+ "Use `import yt_dlp` instead of `import youtube_dl`.[/d]"
155+ )
156+ console .print ()
157+
158+ console .print (
159+ f"[blue]took { (time .time () - START ):.2f} s[/blue]"
160+ )
161+ console .print ()
162+ console .print (
163+ " If you want to save some space for your REPL, this might help:"
164+ )
165+ console .print ()
166+ console .input (
167+ " [green]?[/green] Cleanup installed contents? [blue](Yn)[/blue] "
168+ )
169+
170+ def rmdir (folder : str ):
171+ # https://stackoverflow.com/questions/185936/how-to-delete-the-contents-of-a-folder
172+ for filename in os .listdir (folder ):
173+ file_path = os .path .join (folder , filename )
174+ try :
175+ if os .path .isfile (file_path ) or os .path .islink (file_path ):
176+ os .unlink (file_path )
177+ elif os .path .isdir (file_path ):
178+ shutil .rmtree (file_path )
179+ except Exception as e :
180+ console .print (
181+ '[red]failed [/red] '
182+ 'cannot delete %s. Reason: %s' % (file_path , e )
183+ )
184+ os .rmdir (folder )
185+
186+ rmdir ('opus-1.3.1' )
187+ rmdir ('ffmpeg-6.0-amd64-static' )
188+
189+ console .print ("[blue]all done![/blue]" )
190+
0 commit comments