AstraOS commited on
Commit
b4c8920
1 Parent(s): b5e7d39

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import yt_dlp as youtube_dl
4
+
5
+ def stream_video(youtube_url, stream_key):
6
+ # Extract the video and audio links from the YouTube URL
7
+ ydl_opts = {
8
+ 'format': 'bestvideo+bestaudio/best',
9
+ 'quiet': True,
10
+ }
11
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
12
+ video_info = ydl.extract_info(youtube_url, download=False)
13
+ video_format = ydl.extract_info(youtube_url, download=False)['formats'][0]['format_id']
14
+ video_url = ydl.extract_info(youtube_url, download=False)['url']
15
+ audio_format = ydl.extract_info(video_url, download=False)['formats'][0]['format_id']
16
+ audio_url = ydl.extract_info(video_url, download=False)['url']
17
+
18
+ # Build the ffmpeg command for video streaming
19
+ video_cmd = f'ffmpeg -re -i "{video_url}" -c:v libx264 -preset veryfast -crf 22 -maxrate 4000k -bufsize 8000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 192k -ac 2 -ar 44100 -f flv "rtmp://a.rtmp.youtube.com/live2/{stream_key}"'
20
+
21
+
22
+ # Build the ffmpeg command for audio streaming
23
+ audio_cmd = f'ffmpeg -re -i "{audio_url}" -vn -c:a aac -b:a 192k -ac 2 -ar 44100 -f flv "rtmp://a.rtmp.youtube.com/live2/{stream_key}"'
24
+
25
+
26
+ # Start the video and audio streaming processes
27
+ video_process = subprocess.Popen(video_cmd, shell=True)
28
+ audio_process = subprocess.Popen(audio_cmd, shell=True)
29
+
30
+ # Wait for the processes to finish
31
+ video_process.wait()
32
+ audio_process.wait()
33
+
34
+ # Define the input and output interfaces for Gradio
35
+ inputs = [
36
+ gr.inputs.Textbox(label="YouTube URL", default="https://www.youtube.com/watch?v=dQw4w9WgXcQ"),
37
+ gr.inputs.Textbox(label="Stream Key", default="YOUR_STREAM_KEY"),
38
+ ]
39
+
40
+ outputs = gr.outputs.Textbox(label="Streaming Status")
41
+
42
+ # Create the Gradio app and run it
43
+ app = gr.Interface(fn=stream_video, inputs=inputs, outputs=outputs, title="YouTube Live Streaming with FFmpeg")
44
+ app.launch()