File size: 7,899 Bytes
019f3a5
436734f
 
fd03c78
 
40e04c8
 
5d2e489
8fc67f3
2e469cf
 
 
5c4b237
4478b28
5ff2b1d
5c4b237
8fc67f3
 
 
 
 
 
 
 
ac6e5d2
40e04c8
 
 
 
 
 
 
 
 
 
 
 
 
 
ac6e5d2
7d7426a
 
 
 
e971c05
7d7426a
 
 
 
fd03c78
 
 
 
 
 
 
ac6e5d2
7d7426a
fd03c78
 
 
 
b3f4c7c
2e469cf
 
 
 
b3f4c7c
 
2e469cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3f4c7c
 
 
 
2e469cf
 
 
 
b3f4c7c
8fc67f3
ac6e5d2
 
 
 
4478b28
2e469cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d7426a
 
 
2e469cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d7426a
2e469cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e04c8
2e469cf
 
 
 
2e0eb40
019f3a5
fd03c78
 
2e0eb40
019f3a5
fd03c78
019f3a5
 
fd03c78
 
 
 
 
 
 
40e04c8
 
 
fd03c78
 
 
 
 
 
 
ac6e5d2
fd03c78
 
 
 
 
40e04c8
 
fd03c78
ac6e5d2
 
 
fd03c78
7d7426a
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
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

# Initialize the Hugging Face API with the token
api = HfApi(token=os.getenv("HF_API_TOKEN"))

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)  # Frames per second
    total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))  # Total frames in the video
    duration = int(total_frames / fps) if fps > 0 else 0  # Duration in seconds, as an integer
    video.release()
    return duration

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

def update_index_file(new_metadata_path):
    # Create a temporary directory for cloning
    temp_dir = "temp_repo"
    if os.path.exists(temp_dir):
        shutil.rmtree(temp_dir)
    
    try:
        # Clone the repository
        subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
        
        # Find all JSON files in the metadata directory
        metadata_dir = os.path.join(temp_dir, 'metadata')
        json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
        
        # Create the paths string
        base_url = "huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
        paths = [f"{base_url}{os.path.basename(f)}" for f in json_files]
        
        # Add the new metadata path if it's not already there
        new_metadata_filename = os.path.basename(new_metadata_path)
        new_full_path = f"{base_url}{new_metadata_filename}"
        if new_full_path not in paths:
            paths.append(new_full_path)
        
        # Create the index content
        index_content = "{ " + "; ".join(paths) + "; }"
        
        # Write to a temporary file
        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 index file
        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
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)

def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
    # Create temp paths
    temp_dir = "temp"
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)
    
    try:
        # Generate randomized filenames
        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)
        
        # Write the video content to a file
        with open(video_path, "wb") as f:
            f.write(video_file.read())
        
        # Handle thumbnail
        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
        
        # Get video length
        video_length = get_video_length(video_path)

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

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

# File uploader for video input
uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov"])

if uploaded_video:
    # Show the video details form
    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")
        
        # Optional custom thumbnail uploader
        custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
        
        # Upload button within the form
        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..."):
                    metadata = upload_video_to_hf(
                        uploaded_video, 
                        uploaded_video.name, 
                        title, 
                        description, 
                        uploader,
                        custom_thumbnail
                    )
                    if metadata:
                        st.success("Upload completed successfully!")
                        st.json(metadata)
else:
    st.info("Please upload a video file to begin.")