artificialguybr commited on
Commit
150836e
1 Parent(s): 4e9af05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -47
app.py CHANGED
@@ -1,65 +1,39 @@
1
  import subprocess
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
-
47
- try:
48
- (
49
- ffmpeg
50
- .input(input_file)
51
- .output(output_file, vcodec='libx264', acodec='aac')
52
- .run(overwrite_output=True)
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, mp4_file # Return the file path twice, for both the video and file components
64
 
65
  with gr.Blocks() as app:
@@ -67,19 +41,18 @@ with gr.Blocks() as app:
67
 
68
  with gr.Row():
69
  with gr.Column():
70
- result_video = gr.Video(label="Vídeo Output")
71
- download_link = gr.File(label="Download MP4") # File component for downloading
72
 
73
  with gr.Row():
74
  with gr.Column():
75
  url_input = gr.Textbox(label="Twitch Clip URL")
76
- auth_token_input = gr.Textbox(label="Authentication Token (optional)", type="password")
77
  run_btn = gr.Button("Download Clip")
78
 
79
  run_btn.click(
80
  gradio_interface,
81
- inputs=[url_input, auth_token_input],
82
- outputs=[result_video, download_link] # Include the file component in the outputs
83
  )
84
 
85
  app.queue()
 
1
  import subprocess
2
  import uuid
 
3
  import gradio as gr
4
+ from youtube_dl import YoutubeDL
5
 
6
  title_and_description = """
7
+ # Twitch Clip Downloader
8
  Created by [@artificialguybr](https://artificialguy.com)
9
 
10
+ Enter the Twitch Clip URL and (optionally) an authentication token to download Twitch clips in MP4 format.
11
 
12
  ## Features
13
+ - **Easy to Use**: Simple interface for inputting Twitch clip URLs and optional authentication tokens.
14
+ - **High-Quality Video**: Downloads in the best available quality in MP4 format.
15
  - **Unique File Naming**: Utilizes UUIDs to generate unique file names, avoiding any file overwrite issues.
 
16
 
17
  Feel free to use and generate your own video clips!
18
  """
19
 
20
+ # Function to download Twitch clip using youtube-dl
21
+ def download_twitch_clip(url):
 
22
  unique_id = uuid.uuid4()
23
+ output_filename = f"{unique_id}.mp4"
24
+ ydl_opts = {
25
+ 'format': 'best[ext=mp4]',
26
+ 'outtmpl': output_filename
27
+ }
28
 
29
+ with YoutubeDL(ydl_opts) as ydl:
30
+ ydl.download([url])
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  return output_filename
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # Gradio interface
35
+ def gradio_interface(url):
36
+ mp4_file = download_twitch_clip(url)
 
37
  return mp4_file, mp4_file # Return the file path twice, for both the video and file components
38
 
39
  with gr.Blocks() as app:
 
41
 
42
  with gr.Row():
43
  with gr.Column():
44
+ result_video = gr.Video(label="Video Output")
45
+ download_link = gr.File(label="Download MP4")
46
 
47
  with gr.Row():
48
  with gr.Column():
49
  url_input = gr.Textbox(label="Twitch Clip URL")
 
50
  run_btn = gr.Button("Download Clip")
51
 
52
  run_btn.click(
53
  gradio_interface,
54
+ inputs=[url_input],
55
+ outputs=[result_video, download_link]
56
  )
57
 
58
  app.queue()