artificialguybr commited on
Commit
4d69eaa
·
verified ·
1 Parent(s): 8ab2b6a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import uuid
3
+ import ffmpeg
4
+ import gradio as gr
5
+
6
+ title_and_description = """
7
+ # Download de clipes da Twitch
8
+ Criado por [@artificialguybr] (https://artificialguy.com)
9
+
10
+ Insira o URL do Twitch Clip e (opcionalmente) um token de autenticação para fazer download.
11
+
12
+ ## Recursos
13
+ - **Fácil de usar**: Interface simples para inserir URLs de clipes do Twitch e tokens de autenticação opcionais para baixar VOD que precisa de sub [Veja aqui como obter seu TOKEN de autenticação] (https://twitch-dl.bezdomni.net/commands/download.html#downloading-subscriber-only-vods)
14
+ - **Vídeo de alta qualidade**: Faz download com a melhor qualidade disponível e converte em um formato MP4 amplamente suportado.
15
+ - **Nomenclatura exclusiva de arquivos**: Utiliza UUIDs para gerar nomes de arquivos exclusivos, evitando problemas de substituição de arquivos.
16
+ - **Processamento eficiente**: Utiliza o `ffmpeg-python` para uma conversão de vídeo rápida e confiável.
17
+
18
+ Sinta-se à vontade para usar e gerar seus próprios videoclipes!
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
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="Vídeo Output")
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 Clip")
77
+
78
+ run_btn.click(
79
+ gradio_interface,
80
+ inputs=[url_input, auth_token_input],
81
+ outputs=[result_video]
82
+ )
83
+ app.queue()
84
+ app.launch()