Skip to content

Commit 487f248

Browse files
committed
updated thumbnail for better performance
1 parent 1d9f35c commit 487f248

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

services/v1/video/thumbnail.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,44 @@
1818

1919
import os
2020
import ffmpeg
21-
from services.file_management import download_file
2221
from config import LOCAL_STORAGE_PATH
2322

2423
def extract_thumbnail(video_url, job_id, second=0):
2524
"""
2625
Extract a thumbnail from a video at the specified timestamp.
27-
26+
Uses ffmpeg direct streaming to avoid downloading the entire video file.
27+
2828
Args:
2929
video_url (str): URL of the video to extract thumbnail from
3030
job_id (str): Unique identifier for the job
3131
second (float): Timestamp in seconds to extract the thumbnail from (default: 0)
32-
32+
3333
Returns:
3434
str: Path to the extracted thumbnail image
3535
"""
36-
# Download the video from the provided URL
37-
video_path = download_file(video_url, os.path.join(LOCAL_STORAGE_PATH, f"{job_id}_input"))
38-
3936
# Set output path for the thumbnail
4037
thumbnail_path = os.path.join(LOCAL_STORAGE_PATH, f"{job_id}_thumbnail.jpg")
41-
38+
4239
try:
43-
# Extract thumbnail using ffmpeg at the specified timestamp
40+
# Extract thumbnail directly from URL using ffmpeg streaming
41+
# analyzeduration and probesize are set low to reduce initial buffering
4442
(
4543
ffmpeg
46-
.input(video_path, ss=second) # 'ss' is the seek parameter for the timestamp
47-
.output(thumbnail_path, vframes=1) # vframes=1 extracts a single frame
44+
.input(video_url, ss=second, analyzeduration='100K', probesize='100K')
45+
.output(thumbnail_path, vframes=1, update=1)
4846
.overwrite_output()
4947
.run(capture_stdout=True, capture_stderr=True)
5048
)
51-
52-
# Clean up the downloaded video file
53-
os.remove(video_path)
54-
49+
5550
# Ensure the thumbnail file exists
5651
if not os.path.exists(thumbnail_path):
5752
raise FileNotFoundError(f"Thumbnail file {thumbnail_path} was not created")
58-
53+
5954
return thumbnail_path
60-
55+
6156
except Exception as e:
6257
print(f"Thumbnail extraction failed: {str(e)}")
63-
# Clean up any downloaded files on error
64-
if os.path.exists(video_path):
65-
os.remove(video_path)
58+
# Clean up partial thumbnail file on error
59+
if os.path.exists(thumbnail_path):
60+
os.remove(thumbnail_path)
6661
raise

0 commit comments

Comments
 (0)