Download app.py from LG3370/FB_audio: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/LG3370/FB_audio/resolve/main/app.py
- Command line
-
hf download hf://spaces/LG3370/FB_audio/app.py
-
curl -L -o app.py https://huggingface.co/spaces/LG3370/FB_audio/resolve/main/app.py
2.34 kB
| import gradio as gr | |
| import yt_dlp | |
| import os | |
| import re | |
| import subprocess | |
| def process_video(url, start, end): | |
| try: | |
| # 1. नाम को छोटा रखना (सिर्फ 15 अक्षर) | |
| safe_name = "audio_clip_" + re.sub(r'\W+', '', url)[-10:] | |
| final_filename = f"{safe_name[:15]}.m4a" | |
| # पुरानी फाइलें हटाना | |
| if os.path.exists(final_filename): os.remove(final_filename) | |
| # 2. yt-dlp सेटिंग्स | |
| ydl_opts = { | |
| 'format': 'bestaudio/best', | |
| 'outtmpl': 'temp_full.%(ext)s', | |
| 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', | |
| 'quiet': True, | |
| 'no_warnings': True, | |
| # DNS एरर से बचने के लिए | |
| 'nocheckcertificate': True, | |
| } | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| # डाउनलोड करने की कोशिश | |
| info = ydl.extract_info(url, download=True) | |
| downloaded_file = ydl.prepare_filename(info) | |
| # 3. FFmpeg स्लाइसिंग | |
| ffmpeg_cmd = [ | |
| 'ffmpeg', '-i', downloaded_file, | |
| '-ss', start, '-to', end, | |
| '-c', 'copy', '-y', final_filename | |
| ] | |
| subprocess.run(ffmpeg_cmd, check=True) | |
| # अस्थायी फाइल हटाना | |
| if os.path.exists(downloaded_file): os.remove(downloaded_file) | |
| return final_filename | |
| except Exception as e: | |
| # यहाँ हम Error को स्ट्रिंग के रूप में नहीं, बल्कि एक डमी फाइल में लिखकर भेजेंगे | |
| # ताकि Gradio क्रैश न हो | |
| error_file = "error_log.txt" | |
| with open(error_file, "w") as f: | |
| f.write(str(e)) | |
| return error_file | |
| demo = gr.Interface( | |
| fn=process_video, | |
| inputs=[ | |
| gr.Textbox(label="URL (FB/YT)"), | |
| gr.Textbox(label="Start (00:00:00)", value="00:00:00"), | |
| gr.Textbox(label="End (00:00:10)", value="00:00:10") | |
| ], | |
| outputs=gr.File(label="Output File / Error Log"), | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |