Update app.py
Browse files
app.py
CHANGED
@@ -2,32 +2,45 @@ import subprocess
|
|
2 |
import uuid
|
3 |
import ffmpeg
|
4 |
import gradio as gr
|
5 |
-
import os
|
6 |
-
import re
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def download_twitch_clip(url, auth_token):
|
10 |
-
#
|
11 |
unique_id = uuid.uuid4()
|
12 |
output_filename = f"{unique_id}.mkv"
|
13 |
|
14 |
-
#
|
15 |
command = ["twitch-dl", "download", url, "-q", "source", "-o", output_filename]
|
16 |
|
17 |
-
#
|
18 |
if auth_token.strip():
|
19 |
command.extend(["-a", auth_token])
|
20 |
|
21 |
-
#
|
22 |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
23 |
stdout, stderr = process.communicate()
|
24 |
|
25 |
if process.returncode != 0:
|
26 |
-
raise Exception(f"
|
27 |
|
28 |
return output_filename
|
29 |
|
30 |
-
#
|
31 |
def convert_to_mp4(input_file):
|
32 |
output_file = input_file.replace('.mkv', '.mp4')
|
33 |
|
@@ -40,23 +53,32 @@ def convert_to_mp4(input_file):
|
|
40 |
)
|
41 |
return output_file
|
42 |
except ffmpeg.Error as e:
|
43 |
-
print(f"
|
44 |
return None
|
45 |
|
46 |
-
#
|
47 |
def gradio_interface(url, auth_token=""):
|
48 |
mkv_file = download_twitch_clip(url, auth_token)
|
49 |
mp4_file = convert_to_mp4(mkv_file)
|
50 |
return mp4_file
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
gr.
|
57 |
-
|
58 |
-
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import uuid
|
3 |
import ffmpeg
|
4 |
import gradio as gr
|
|
|
|
|
5 |
|
6 |
+
title_and_description = """
|
7 |
+
# Twitch Clip Downloader and Converter
|
8 |
+
Created by [@artificialguybr](https://artificialguy.com)
|
9 |
+
|
10 |
+
Enter the Twitch Clip URL and (optionally) an authentication token to download and convert Twitch clips from MKV to MP4 format.
|
11 |
+
|
12 |
+
## Features
|
13 |
+
- **Easy to Use**: Simple interface for inputting Twitch clip URLs and optional authentication tokens to download subscription VOD [See here how to get your Authentication TOKEN](https://twitch-dl.bezdomni.net/commands/download.html#downloading-subscriber-only-vods)
|
14 |
+
- **High-Quality Video**: Downloads in the best available quality and converts to a widely supported MP4 format.
|
15 |
+
- **Unique File Naming**: Utilizes UUIDs to generate unique file names, avoiding any file overwrite issues.
|
16 |
+
- **Efficient Processing**: Leverages `ffmpeg-python` for fast and reliable video conversion.
|
17 |
+
|
18 |
+
Feel free to use and generate your own video clips!
|
19 |
+
"""
|
20 |
+
|
21 |
+
# Function to download Twitch clip using twitch-dl
|
22 |
def download_twitch_clip(url, auth_token):
|
23 |
+
# Generate a UUID for the file name
|
24 |
unique_id = uuid.uuid4()
|
25 |
output_filename = f"{unique_id}.mkv"
|
26 |
|
27 |
+
# Command to download the video
|
28 |
command = ["twitch-dl", "download", url, "-q", "source", "-o", output_filename]
|
29 |
|
30 |
+
# Add authentication token if provided
|
31 |
if auth_token.strip():
|
32 |
command.extend(["-a", auth_token])
|
33 |
|
34 |
+
# Execute the download command
|
35 |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
36 |
stdout, stderr = process.communicate()
|
37 |
|
38 |
if process.returncode != 0:
|
39 |
+
raise Exception(f"Error in video download: {stderr.decode('utf-8')}")
|
40 |
|
41 |
return output_filename
|
42 |
|
43 |
+
# Function to convert MKV to MP4 using ffmpeg-python
|
44 |
def convert_to_mp4(input_file):
|
45 |
output_file = input_file.replace('.mkv', '.mp4')
|
46 |
|
|
|
53 |
)
|
54 |
return output_file
|
55 |
except ffmpeg.Error as e:
|
56 |
+
print(f"Error in file conversion: {e}")
|
57 |
return None
|
58 |
|
59 |
+
# Gradio interface
|
60 |
def gradio_interface(url, auth_token=""):
|
61 |
mkv_file = download_twitch_clip(url, auth_token)
|
62 |
mp4_file = convert_to_mp4(mkv_file)
|
63 |
return mp4_file
|
64 |
|
65 |
+
with gr.Blocks() as app:
|
66 |
+
gr.Markdown(title_and_description)
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
with gr.Column():
|
70 |
+
result_video = gr.Video(label="Converted Video")
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
url_input = gr.Textbox(label="Twitch Clip URL")
|
75 |
+
auth_token_input = gr.Textbox(label="Authentication Token (optional)", type="password")
|
76 |
+
run_btn = gr.Button("Download and Convert")
|
77 |
+
|
78 |
+
run_btn.click(
|
79 |
+
gradio_interface,
|
80 |
+
inputs=[url_input, auth_token_input],
|
81 |
+
outputs=[result_video]
|
82 |
+
)
|
83 |
+
|
84 |
+
app.launch()
|