File size: 12,354 Bytes
019f3a5
436734f
 
fd03c78
 
40e04c8
 
5d2e489
8fc67f3
2e469cf
 
 
7a2d05f
 
 
5c4b237
7a2d05f
 
 
5c4b237
8fc67f3
 
 
 
 
 
 
 
ac6e5d2
40e04c8
 
 
 
 
 
 
 
 
 
 
 
 
 
ac6e5d2
7d7426a
 
7a2d05f
 
 
7d7426a
 
 
7a2d05f
fd03c78
 
 
 
 
 
 
ac6e5d2
7a2d05f
 
fd03c78
 
 
 
b3f4c7c
2e469cf
292c192
 
2e469cf
 
b3f4c7c
 
0ab392d
292c192
0ab392d
 
 
292c192
 
2e469cf
 
 
292c192
7c2caee
2e469cf
292c192
7c2caee
 
 
 
 
 
2e469cf
 
7c2caee
 
 
 
 
2e469cf
292c192
 
 
 
7c2caee
2e469cf
292c192
2e469cf
 
 
 
 
292c192
7a2d05f
2e469cf
 
b3f4c7c
 
 
 
2e469cf
292c192
2e469cf
 
b3f4c7c
bf20bee
7a2d05f
 
 
 
 
292c192
7a2d05f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf20bee
7a2d05f
 
292c192
cbd93b0
ac6e5d2
 
 
4478b28
2e469cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d7426a
7a2d05f
cbd93b0
 
 
 
7a2d05f
 
bf20bee
cbd93b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d7426a
7a2d05f
2e469cf
7a2d05f
2e469cf
 
 
 
 
 
 
7a2d05f
2e469cf
 
 
 
 
 
7a2d05f
 
2e469cf
 
 
 
7a2d05f
2e469cf
 
 
 
 
 
 
 
 
40e04c8
2e469cf
 
 
2e0eb40
019f3a5
fd03c78
 
2e0eb40
7a2d05f
019f3a5
 
fd03c78
 
 
 
 
 
7a2d05f
 
 
 
 
 
2a12f47
 
0ab392d
a5191e9
292c192
 
a5191e9
bf20bee
292c192
a5191e9
292c192
 
7a2d05f
40e04c8
 
fd03c78
 
 
 
 
 
7a2d05f
fd03c78
 
 
 
 
40e04c8
bf20bee
40e04c8
fd03c78
ac6e5d2
 
 
fd03c78
7a2d05f
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import streamlit as st
from huggingface_hub import HfApi
import os
import json
from datetime import datetime
import cv2
import random
from PIL import Image
import string
import subprocess
import glob
import shutil
from groq import Groq
import tempfile
from pydub import AudioSegment

# Initialize the Hugging Face and Groq APIs
hf_api = HfApi(token=os.getenv("HF_API_TOKEN"))
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))

def generate_random_string(length=4):
    return ''.join(random.choices(string.ascii_lowercase, k=length))

def add_random_to_filename(filename):
    name, ext = os.path.splitext(filename)
    random_string = generate_random_string()
    return f"{name}-{random_string}{ext}"

def extract_thumbnail(video_path, thumbnail_path):
    video = cv2.VideoCapture(video_path)
    total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    random_frame = random.randint(0, total_frames - 1)
    video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
    success, frame = video.read()
    if success:
        cv2.imwrite(thumbnail_path, frame)
    video.release()
    return success

def save_custom_thumbnail(thumbnail_file, thumbnail_path):
    img = Image.open(thumbnail_file)
    img.save(thumbnail_path)
    return True

def get_video_length(video_path):
    video = cv2.VideoCapture(video_path)
    fps = video.get(cv2.CAP_PROP_FPS)
    total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    duration = int(total_frames / fps) if fps > 0 else 0
    video.release()
    return duration

def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location, subtitle_location, duration):
    return {
        "fileName": video_name,
        "title": title,
        "description": description,
        "uploader": uploader,
        "uploadTimestamp": datetime.now().isoformat(),
        "fileLocation": file_location,
        "thumbnailLocation": thumbnail_location,
        "subtitleLocation": subtitle_location,
        "duration": duration,
        "views": 0,
        "likes": 0
    }

def update_index_file(new_metadata_path):
    temp_dir = "temp_repo"
    
    # Remove existing temp directory if it exists
    if os.path.exists(temp_dir):
        shutil.rmtree(temp_dir)
    
    try:
        
        # Clone the Hugging Face repo
        subprocess.run('GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/spaces/vericudebuget/ok4231 ' + temp_dir, 
               shell=True, 
               check=True)
        
        # Find all existing JSON metadata files
        metadata_dir = os.path.join(temp_dir, 'metadata')
        json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
        
        base_url = "https://huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
        paths = []
        
        # Collect existing metadata files with timestamps
        for f in json_files:
            file_timestamp = datetime.now().isoformat()  # Get the current timestamp
            file_path = f"{base_url}{os.path.basename(f)}"
            paths.append({"url": file_path, "timestamp": file_timestamp})
        
        # Add the new metadata file with the current timestamp
        new_metadata_filename = os.path.basename(new_metadata_path)
        new_full_path = f"{base_url}{new_metadata_filename}"
        file_timestamp = datetime.now().isoformat()  # Get timestamp for the new metadata file
        
        # Check if the new file is already in the list, if not, add it
        if not any(entry['url'] == new_full_path for entry in paths):
            paths.append({"url": new_full_path, "timestamp": file_timestamp})
        
        # Sort the paths by timestamp in descending order (latest to oldest)
        paths.sort(key=lambda x: x['timestamp'], reverse=True)
        
        # Convert the paths list to a JSON format
        index_content = json.dumps(paths, indent=2)
        
        # Write the sorted index to 'video-index.json'
        index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
        os.makedirs(os.path.dirname(index_path), exist_ok=True)
        with open(index_path, 'w') as f:
            f.write(index_content)
        
        # Upload the updated index file to the Hugging Face space
        hf_api.upload_file(
            path_or_fileobj=index_path,
            path_in_repo="metadata/video-index.json",
            repo_id="vericudebuget/ok4231",
            repo_type="space",
        )
    
    finally:
        # Clean up by removing the temp directory
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)

def create_subtitles(video_path):  # Renamed from generate_subtitles
    with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_audio:
        # Convert video to mono 128kbps MP3
        audio = AudioSegment.from_file(video_path)
        audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)
        audio.export(temp_audio.name, format='mp3', bitrate='128k')

        # Generate subtitles using Groq
        with open(temp_audio.name, 'rb') as audio_file:
            translation = groq_client.audio.translations.create(
                file=(temp_audio.name, audio_file.read()),
                model="whisper-large-v3",
                response_format="verbose_json",
                temperature=0.0
            )
    
    # Generate VTT content
    vtt_content = "WEBVTT\n\n"
    for segment in translation.segments:
        start_time = segment['start']
        end_time = segment['end']
        text = segment['text'].strip()
        
        start_time_vtt = f"{int(start_time // 3600):02}:{int((start_time % 3600) // 60):02}:{start_time % 60:06.3f}"
        end_time_vtt = f"{int(end_time // 3600):02}:{int((end_time % 3600) // 60):02}:{end_time % 60:06.3f}"
        
        vtt_content += f"{start_time_vtt} --> {end_time_vtt}\n{text}\n\n"
    
    os.unlink(temp_audio.name)  # Clean up temp file
    return vtt_content


def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None):
    temp_dir = "temp"
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)
    
    try:
        video_name = add_random_to_filename(original_video_name)
        video_path = os.path.join(temp_dir, video_name)
        
        base_name = os.path.splitext(video_name)[0]
        thumbnail_name = f"{base_name}_thumb.jpg"
        thumbnail_path = os.path.join(temp_dir, thumbnail_name)
        
        json_name = f"{base_name}-index.json"
        json_path = os.path.join(temp_dir, json_name)
        
        with open(video_path, "wb") as f:
            f.write(video_file.read())
        
        if custom_thumbnail:
            thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
        else:
            thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
        
        if not thumbnail_extracted:
            st.error("Failed to process thumbnail")
            return None
        
        video_length = get_video_length(video_path)
        
        # Analyze audio level
        audio = AudioSegment.from_file(video_path)
        audio_dBFS = audio.dBFS
        
        # Generate and upload subtitles if requested and video is not too long
        subtitle_location = ""
        if should_generate_subs and video_length <= 3600:  # 1 hour in seconds
            if audio_dBFS < -90:
                subtitle_location = ""  # Set to empty if audio is too quiet
            else:
                try:
                    vtt_content = create_subtitles(video_path)  # Using renamed function
                    subtitle_name = f"{base_name}.vtt"
                    subtitle_path = os.path.join(temp_dir, subtitle_name)
                    
                    with open(subtitle_path, 'w') as f:
                        f.write(vtt_content)
                    
                    subtitle_location = f"subtitles/{subtitle_name}"
                    hf_api.upload_file(
                        path_or_fileobj=subtitle_path,
                        path_in_repo=subtitle_location,
                        repo_id="vericudebuget/ok4231",
                        repo_type="space",
                    )
                except Exception as e:
                    st.warning(f"Failed to generate subtitles: {str(e)}")

        # Upload video and thumbnail
        video_location = f"videos/{video_name}"
        hf_api.upload_file(
            path_or_fileobj=video_path,
            path_in_repo=video_location,
            repo_id="vericudebuget/ok4231",
            repo_type="space",
        )
        
        thumbnail_location = f"thumbnails/{thumbnail_name}"
        hf_api.upload_file(
            path_or_fileobj=thumbnail_path,
            path_in_repo=thumbnail_location,
            repo_id="vericudebuget/ok4231",
            repo_type="space",
        )
        
        # Generate and upload metadata
        metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location, subtitle_location, video_length)
        with open(json_path, "w") as f:
            json.dump(metadata, f, indent=2)
        
        metadata_location = f"metadata/{json_name}"
        hf_api.upload_file(
            path_or_fileobj=json_path,
            path_in_repo=metadata_location,
            repo_id="vericudebuget/ok4231",
            repo_type="space",
        )
        
        update_index_file(metadata_location)
        
        return metadata
    
    finally:
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)

# Streamlit app interface
st.title("Upload your video")
st.markdown("---")

uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov", "webm", "mkv"])

if uploaded_video:
    with st.form("video_details"):
        st.write("Video Details")
        title = st.text_input("Title", placeholder="Enter video title")
        description = st.text_area("Description", placeholder="Enter video description")
        uploader = st.text_input("Uploader Name", placeholder="Enter your name")
        
        # Create a temporary file to get video duration
        with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
            temp_video.write(uploaded_video.getvalue())
            video_duration = get_video_length(temp_video.name)
        os.unlink(temp_video.name)  # Clean up temp file
        
        # Subtitle generation toggle, disabled if video is longer than 2 hours
        should_generate_subs = st.toggle("Generate Subtitles. - If enabled, the subtitles will automatically be translated into English.", disabled=video_duration > 7200, value=True)  # Renamed variable
        
        
        if video_duration > 1180:
            st.warning("Hey there! Just wanted to warn you that uploading pirated movies is not allowed.")
        
        if video_duration > 3600 and should_generate_subs:
            st.warning("Warning, for videos longer than an hour, generating subtitles will take some time! Please wait :)")
        
        if video_duration > 7000:
            st.warning("Now that's a long video. It will take a long time to upload. Make sure you have the right uploader details!")
        
        custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
        
        submit_button = st.form_submit_button("Upload Video")
        
        if submit_button:
            if not title or not uploader:
                st.error("Please fill in the title and uploader name.")
            else:
                with st.spinner("Uploading video, generating thumbnail and metadata... This may take some time. Please wait."):
                    metadata = upload_video_to_hf(
                        uploaded_video, 
                        uploaded_video.name, 
                        title, 
                        description, 
                        uploader,
                        should_generate_subs,  # Using renamed variable
                        custom_thumbnail
                    )
                    if metadata:
                        st.success("Upload completed successfully!")
                        st.json(metadata)
else:
    st.info("Please upload a video file to begin.")