| import gradio as gr |
| import yt_dlp |
| import os |
| import re |
| import subprocess |
|
|
| def process_video(url, start, end): |
| try: |
| |
| 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) |
|
|
| |
| 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, |
| |
| 'nocheckcertificate': True, |
| } |
|
|
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
| |
| info = ydl.extract_info(url, download=True) |
| downloaded_file = ydl.prepare_filename(info) |
|
|
| |
| 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_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() |