Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| from utils import download_media_from_url, convert_media | |
| def handle_url_input(url): | |
| """Downloads media from a URL and returns the path.""" | |
| if not url: | |
| raise gr.Error("Please enter a valid URL") | |
| try: | |
| gr.Info("Downloading media from URL...") | |
| file_path = download_media_from_url(url) | |
| return file_path, file_path # Return to state and video preview | |
| except Exception as e: | |
| raise gr.Error(f"Download failed: {str(e)}") | |
| def handle_file_upload(file): | |
| """Handles direct file upload.""" | |
| if not file: | |
| return None, None | |
| return file, file | |
| def process_conversion(input_path, target_format): | |
| """Orchestrates the conversion process.""" | |
| if not input_path: | |
| raise gr.Error("No media file selected. Please upload a file or enter a URL.") | |
| gr.Info(f"Converting to {target_format}...") | |
| try: | |
| output_path, is_audio = convert_media(input_path, target_format) | |
| # Return logic: (Video_Component, Audio_Component, File_Download_Component) | |
| # We toggle visibility based on whether the result is audio or video | |
| if is_audio: | |
| return { | |
| video_output: gr.Video(visible=False), | |
| audio_output: gr.Audio(value=output_path, visible=True), | |
| file_output: gr.File(value=output_path, visible=True) | |
| } | |
| else: | |
| return { | |
| video_output: gr.Video(value=output_path, visible=True), | |
| audio_output: gr.Audio(visible=False), | |
| file_output: gr.File(value=output_path, visible=True) | |
| } | |
| except Exception as e: | |
| raise gr.Error(f"Conversion failed: {str(e)}") | |
| # --- Gradio 6 Application Definition --- | |
| with gr.Blocks( | |
| title="Universal Media Converter", | |
| theme=gr.themes.Soft(), | |
| footer_links=[ | |
| {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}, | |
| {"label": "Documentation", "url": "https://www.gradio.app"} | |
| ] | |
| ) as demo: | |
| # State to hold the path of the currently loaded file (whether from upload or URL) | |
| current_file_state = gr.State() | |
| with gr.Row(): | |
| gr.Markdown( | |
| """ | |
| # π Universal Media Converter | |
| [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder) | |
| Convert video files or social media links (Twitter/X, etc.) to various formats including GIF, MP4, MP3, and WEBM. | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| with gr.Tabs(): | |
| with gr.Tab("π Upload File"): | |
| upload_input = gr.File( | |
| label="Drop Video File", | |
| file_types=["video", "audio"], | |
| type="filepath" | |
| ) | |
| with gr.Tab("π From URL"): | |
| url_input = gr.Textbox( | |
| label="Paste URL (Twitter/X, etc.)", | |
| placeholder="https://twitter.com/..." | |
| ) | |
| url_btn = gr.Button("Fetch Media", variant="secondary") | |
| # Preview of the input | |
| input_preview = gr.Video(label="Input Preview", interactive=False) | |
| with gr.Column(scale=1): | |
| gr.Markdown("### Conversion Settings") | |
| target_format = gr.Dropdown( | |
| choices=[ | |
| "MP4", "WEBM", "GIF", "AVI", "MOV", "MKV", # Video | |
| "MP3", "WAV", "FLAC", "AAC", "OGG" # Audio | |
| ], | |
| value="MP4", | |
| label="Target Format", | |
| info="Select video or audio output format" | |
| ) | |
| convert_btn = gr.Button("π Convert Now", variant="primary", size="lg") | |
| # Output Area | |
| gr.Markdown("### Result") | |
| video_output = gr.Video(label="Video Output", visible=False) | |
| audio_output = gr.Audio(label="Audio Output", visible=False) | |
| file_output = gr.File(label="Download Converted File") | |
| # --- Event Listeners --- | |
| # 1. Handle URL Fetching | |
| url_btn.click( | |
| fn=handle_url_input, | |
| inputs=[url_input], | |
| outputs=[current_file_state, input_preview], | |
| api_visibility="public" | |
| ) | |
| # 2. Handle File Upload | |
| upload_input.upload( | |
| fn=handle_file_upload, | |
| inputs=[upload_input], | |
| outputs=[current_file_state, input_preview], | |
| api_visibility="public" | |
| ) | |
| # 3. Handle Conversion | |
| convert_btn.click( | |
| fn=process_conversion, | |
| inputs=[current_file_state, target_format], | |
| outputs=[video_output, audio_output, file_output], | |
| api_visibility="public" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |