File size: 2,362 Bytes
69c11ea
98b6fef
 
 
 
45b4ac0
98b6fef
 
 
 
 
8c9c705
090c7e0
 
98b6fef
 
5ca1c96
8c9c705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f10c2d2
1a92ad7
98b6fef
f10c2d2
98b6fef
 
 
8c9c705
98b6fef
090c7e0
98b6fef
8c9c705
98b6fef
 
090c7e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import yt_dlp
import gradio as gr
import subprocess
from uuid import uuid4
from time import sleep
from tempfile import TemporaryDirectory

cachedir = ".cache"


def clip(yt_url, video_file_path, start_timestamp, end_timestamp):
    start_timestamp = int(start_timestamp)
    duration = int(end_timestamp) - int(start_timestamp)
    
    with TemporaryDirectory() as tmpdir:
        if yt_url is not None and len(yt_url.strip()) != 0:
            video_output_path = os.path.join(tmpdir, "video.mp4")
            ydl_opts = {
                "quiet": True,
                "outtmpl": video_output_path,
                "postprocessors": [{"key": "FFmpegVideoConvertor", "preferedformat": "mp4"}],
                "cachedir": cachedir
            }
            if not os.path.exists(video_output_path):
                try:
                    ydl_opts["format"] = "22"
                    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                        ydl.download([yt_url])
                except Exception as e:
                    print(e)
                    ydl_opts["format"] = "bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480][ext=mp4]/best"
                    sleep(5)
                    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                        ydl.download([yt_url])
        elif os.path.exists(video_file_path):
            video_output_path = video_file_path
        else:
            raise Exception("Select valid input.")
        clip_path = f"{uuid4()}.mp3"
        print(f'ffmpeg -ss {start_timestamp} -t {duration} -i "{video_output_path}" "{clip_path}"')
        subprocess.run(f'ffmpeg -ss {start_timestamp} -t {duration} -i "{video_output_path}" "{clip_path}"', shell=True)
    return clip_path


url_link = gr.Textbox(label="YouTube URL")
video_file = gr.Video(label="Upload Video")
start_timestamp = gr.Textbox(label="Clip start timestamp (in seconds)")
end_timestamp = gr.Textbox(label="Clip end timestamp (in seconds)")

interface = gr.Interface(clip, [url_link, video_file, start_timestamp, end_timestamp], "audio", title="YouTube / Video Clipper", description="After downloading the clips, please go to https://demo.deepsync.co/11-voice-cloning to clone the voice.")

if __name__=="__main__":
    interface.queue().launch(auth=(os.environ.get("GRADIO_USERNAME"), os.environ.get("GRADIO_PASSWORD")))