Natkat1 commited on
Commit
a17739e
·
verified ·
1 Parent(s): ea70375

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +148 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from utils import download_media_from_url, convert_media
4
+
5
+ def handle_url_input(url):
6
+ """Downloads media from a URL and returns the path."""
7
+ if not url:
8
+ raise gr.Error("Please enter a valid URL")
9
+
10
+ try:
11
+ gr.Info("Attempting download... This may take a few seconds.")
12
+ file_path = download_media_from_url(url)
13
+ return file_path, file_path # Return to state and video preview
14
+ except Exception as e:
15
+ # Friendly error message for common issues
16
+ error_msg = str(e)
17
+ if "403" in error_msg or "Forbidden" in error_msg:
18
+ raise gr.Error("Access denied (403). The site might be blocking automated downloads from this server IP.")
19
+ elif "Video unavailable" in error_msg:
20
+ raise gr.Error("Video not found. It might be private or deleted.")
21
+ else:
22
+ raise gr.Error(f"Download failed: {error_msg}")
23
+
24
+ def handle_file_upload(file):
25
+ """Handles direct file upload."""
26
+ if not file:
27
+ return None, None
28
+ return file, file
29
+
30
+ def process_conversion(input_path, target_format):
31
+ """Orchestrates the conversion process."""
32
+ if not input_path:
33
+ raise gr.Error("No media file selected. Please upload a file or enter a URL.")
34
+
35
+ gr.Info(f"Converting to {target_format}...")
36
+
37
+ try:
38
+ output_path, is_audio = convert_media(input_path, target_format)
39
+
40
+ # Return logic: (Video_Component, Audio_Component, File_Download_Component)
41
+ if is_audio:
42
+ return {
43
+ video_output: gr.Video(visible=False),
44
+ audio_output: gr.Audio(value=output_path, visible=True),
45
+ file_output: gr.File(value=output_path, visible=True)
46
+ }
47
+ else:
48
+ return {
49
+ video_output: gr.Video(value=output_path, visible=True),
50
+ audio_output: gr.Audio(visible=False),
51
+ file_output: gr.File(value=output_path, visible=True)
52
+ }
53
+
54
+ except Exception as e:
55
+ raise gr.Error(f"Conversion failed: {str(e)}")
56
+
57
+ # --- Gradio 6 Application Definition ---
58
+
59
+ with gr.Blocks(
60
+ title="Universal Media Converter",
61
+ theme=gr.themes.Soft(),
62
+ footer_links=[
63
+ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
64
+ {"label": "yt-dlp Documentation", "url": "https://github.com/yt-dlp/yt-dlp"}
65
+ ]
66
+ ) as demo:
67
+
68
+ # State to hold the path of the currently loaded file
69
+ current_file_state = gr.State()
70
+
71
+ with gr.Row():
72
+ gr.Markdown(
73
+ """
74
+ # 🔄 Universal Media Converter
75
+ [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
76
+
77
+ Robust downloader and converter for Twitter/X, YouTube, TikTok, and more.
78
+ """
79
+ )
80
+
81
+ with gr.Row():
82
+ with gr.Column(scale=1):
83
+ with gr.Tabs():
84
+ with gr.Tab("🔗 From URL"):
85
+ gr.Markdown("Paste a link from X (Twitter), YouTube, or TikTok.")
86
+ url_input = gr.Textbox(
87
+ label="Paste URL",
88
+ placeholder="https://x.com/user/status/...",
89
+ show_label=False
90
+ )
91
+ url_btn = gr.Button("Fetch Media", variant="primary")
92
+
93
+ with gr.Tab("📁 Upload File"):
94
+ upload_input = gr.File(
95
+ label="Drop Video File",
96
+ file_types=["video", "audio"],
97
+ type="filepath"
98
+ )
99
+
100
+ # Preview of the input
101
+ input_preview = gr.Video(label="Input Preview", interactive=False)
102
+
103
+ with gr.Column(scale=1):
104
+ gr.Markdown("### Conversion Settings")
105
+
106
+ target_format = gr.Dropdown(
107
+ choices=[
108
+ "MP4", "WEBM", "GIF", "AVI", "MOV", "MKV", # Video
109
+ "MP3", "WAV", "FLAC", "AAC", "OGG" # Audio
110
+ ],
111
+ value="MP4",
112
+ label="Target Format",
113
+ info="Select output format"
114
+ )
115
+
116
+ convert_btn = gr.Button("🚀 Convert Now", variant="secondary", size="lg")
117
+
118
+ # Output Area
119
+ gr.Markdown("### Result")
120
+ video_output = gr.Video(label="Video Output", visible=False)
121
+ audio_output = gr.Audio(label="Audio Output", visible=False)
122
+ file_output = gr.File(label="Download Converted File")
123
+
124
+ # --- Event Listeners ---
125
+
126
+ url_btn.click(
127
+ fn=handle_url_input,
128
+ inputs=[url_input],
129
+ outputs=[current_file_state, input_preview],
130
+ api_visibility="public"
131
+ )
132
+
133
+ upload_input.upload(
134
+ fn=handle_file_upload,
135
+ inputs=[upload_input],
136
+ outputs=[current_file_state, input_preview],
137
+ api_visibility="public"
138
+ )
139
+
140
+ convert_btn.click(
141
+ fn=process_conversion,
142
+ inputs=[current_file_state, target_format],
143
+ outputs=[video_output, audio_output, file_output],
144
+ api_visibility="public"
145
+ )
146
+
147
+ if __name__ == "__main__":
148
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ requests
3
+ Pillow
4
+ numpy
5
+ opencv-python
6
+ moviepy
7
+ pandas