File size: 3,463 Bytes
485608a
faa2bdb
c8942d9
79683ca
faa2bdb
7a80d3a
5296733
40446f2
79683ca
e4ed31e
40446f2
ec46e62
 
 
701ed9a
 
 
b7d9c54
131761b
e4ed31e
 
79683ca
e4ed31e
79683ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ed31e
79683ca
 
 
3c753ee
79683ca
 
 
3c753ee
79683ca
 
35a1468
79683ca
3c753ee
79683ca
 
e4ed31e
3c753ee
 
 
79683ca
3c753ee
79683ca
 
35a1468
79683ca
 
 
 
 
e4ed31e
3c753ee
 
79683ca
 
0dcabf9
79683ca
 
0dcabf9
6f1f7b0
79683ca
 
6f1f7b0
79683ca
 
 
0dcabf9
6f1f7b0
79683ca
e4ed31e
 
 
 
 
79683ca
 
e4ed31e
79683ca
 
e4ed31e
79683ca
 
0dcabf9
79683ca
 
 
 
 
 
 
 
0dcabf9
 
79683ca
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import time
import os
import gradio as gr
import requests
import subprocess
from gradio import components
import tempfile
from pathlib import Path
from gradio import inputs, outputs
from moviepy.editor import VideoFileClip

# Define File object
File = Path

# Define temp directory
temp_dir = tempfile.mkdtemp()

video_file = gr.inputs.File(label="Video File", type="file")
quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")

# default will now be None, which will retain original aspect ratio
aspect_ratio = gr.inputs.Dropdown(choices=[
  None,  # Add None option to retain original aspect ratio
  "1:1",
  "4:3",
  "3:2",
  "5:4",
  "16:9",
  "21:9",
  "1.85:1",
  "2.35:1",
  "3:1",
  "360",
  "9:16",
  "16:9",
  "2:1",
  "1:2",
  "9:1",
], label="Aspect Ratio", default=None) 

video_url = gr.inputs.Textbox(label="Video URL")

def convert_video(video_file, quality, aspect_ratio, video_url):
    # If a file was uploaded
    if video_file is not None:
        # Get the file extension
        file_extension = video_file.name.split(".")[-1]

        # Define output file name
        output_file_name = video_file.name[:-len(file_extension) - 1] + ".m3u8"
        
        input_path = video_file.name
    
    # If a URL was provided
    elif video_url:
        # Get the file name from URL
        file_name = video_url.split("/")[-1]

        # Get the file extension
        file_extension = file_name.split(".")[-1]

        # Define output file name
        output_file_name = file_name[:-len(file_extension) - 1] + ".m3u8"
        
        # Download the file to the temporary directory
        response = requests.get(video_url)
        
        if response.status_code == 200:
            input_path = os.path.join(temp_dir, file_name)
            with open(input_path, 'wb') as f:
                f.write(response.content)
        else:
            print("Failed to download file from URL.")
            return "Failed to download file from URL."
    else:
        print("No input was provided.")
        return "No input was provided."

    # Define the output path
    output_path = os.path.join(temp_dir, output_file_name)

    # Check if the output file exists
    if os.path.exists(output_path):
        # The file already exists, so we can just display it in the output viewer
        return output_path
    else:
        # The file does not exist, so we need to convert it
        if aspect_ratio is None:
            video = VideoFileClip(input_path)
            aspect_ratio = f"{video.size[0]}:{video.size[1]}"
        
        ffmpeg_command = f"ffmpeg -i {input_path} -c:v libx264 -crf {quality} -vf scale=-1:720,setsar={aspect_ratio} -hls_time 6 -hls_playlist_type vod -f hls {output_path}"

        try:
            subprocess.run(ffmpeg_command, shell=True, timeout=600)
        except subprocess.TimeoutExpired:
            print("ffmpeg timed out")
            return "ffmpeg command timed out."
        except FileNotFoundError:
            print("ffmpeg is not installed.")
            return "ffmpeg is not installed."

        for i in range(10):
            if os.path.exists(output_path):
                break
            else:
                time.sleep(1)

        # The file has now been converted, so we can display it in the output viewer
        return output_path

gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()