vericudebuget commited on
Commit
b3f4c7c
·
verified ·
1 Parent(s): 8fc67f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -10
app.py CHANGED
@@ -21,21 +21,12 @@ def add_random_to_filename(filename):
21
 
22
  def extract_thumbnail(video_path, thumbnail_path):
23
  video = cv2.VideoCapture(video_path)
24
-
25
- # Get total number of frames
26
  total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
27
-
28
- # Choose a random frame
29
  random_frame = random.randint(0, total_frames - 1)
30
-
31
- # Set the frame position
32
  video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
33
-
34
- # Read the frame
35
  success, frame = video.read()
36
  if success:
37
  cv2.imwrite(thumbnail_path, frame)
38
-
39
  video.release()
40
  return success
41
 
@@ -57,6 +48,43 @@ def generate_metadata(video_name, title, description, uploader, file_location, t
57
  "likes": 0
58
  }
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
61
  # Create temp paths
62
  temp_dir = "temp"
@@ -111,13 +139,17 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
111
  with open(json_path, "w") as f:
112
  json.dump(metadata, f, indent=2)
113
 
 
114
  api.upload_file(
115
  path_or_fileobj=json_path,
116
- path_in_repo=f"metadata/{json_name}",
117
  repo_id="vericudebuget/ok4231",
118
  repo_type="space",
119
  )
120
 
 
 
 
121
  # Cleanup temp files
122
  for file_path in [video_path, thumbnail_path, json_path]:
123
  if os.path.exists(file_path):
 
21
 
22
  def extract_thumbnail(video_path, thumbnail_path):
23
  video = cv2.VideoCapture(video_path)
 
 
24
  total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
 
 
25
  random_frame = random.randint(0, total_frames - 1)
 
 
26
  video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
 
 
27
  success, frame = video.read()
28
  if success:
29
  cv2.imwrite(thumbnail_path, frame)
 
30
  video.release()
31
  return success
32
 
 
48
  "likes": 0
49
  }
50
 
51
+ def update_index_file(new_metadata_path):
52
+ index_file_path = "metadata/index-video.json"
53
+
54
+ try:
55
+ # Try to download existing index file
56
+ existing_content = api.download_file(
57
+ repo_id="vericudebuget/ok4231",
58
+ repo_type="space",
59
+ path_in_repo=index_file_path
60
+ )
61
+ index_data = json.loads(existing_content.decode())
62
+ except:
63
+ # If file doesn't exist or can't be loaded, start with empty list
64
+ index_data = {"metadata_files": []}
65
+
66
+ # Add new metadata path if it's not already in the list
67
+ if new_metadata_path not in index_data["metadata_files"]:
68
+ index_data["metadata_files"].append(new_metadata_path)
69
+
70
+ # Create temporary file for updated index
71
+ temp_index_path = "temp/index-video.json"
72
+ os.makedirs(os.path.dirname(temp_index_path), exist_ok=True)
73
+
74
+ with open(temp_index_path, "w") as f:
75
+ json.dump(index_data, f)
76
+
77
+ # Upload updated index file
78
+ api.upload_file(
79
+ path_or_fileobj=temp_index_path,
80
+ path_in_repo=index_file_path,
81
+ repo_id="vericudebuget/ok4231",
82
+ repo_type="space",
83
+ )
84
+
85
+ # Clean up temporary file
86
+ os.remove(temp_index_path)
87
+
88
  def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
89
  # Create temp paths
90
  temp_dir = "temp"
 
139
  with open(json_path, "w") as f:
140
  json.dump(metadata, f, indent=2)
141
 
142
+ metadata_location = f"metadata/{json_name}"
143
  api.upload_file(
144
  path_or_fileobj=json_path,
145
+ path_in_repo=metadata_location,
146
  repo_id="vericudebuget/ok4231",
147
  repo_type="space",
148
  )
149
 
150
+ # Update the index file
151
+ update_index_file(metadata_location)
152
+
153
  # Cleanup temp files
154
  for file_path in [video_path, thumbnail_path, json_path]:
155
  if os.path.exists(file_path):