Skip to content

Commit f448043

Browse files
authored
[None][feat] Support base64 video input (#8458)
Signed-off-by: Wanli Jiang <[email protected]>
1 parent e666a70 commit f448043

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

tensorrt_llm/inputs/utils.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async def async_load_image(
124124
return image
125125

126126

127-
def load_video(
127+
def _load_video_by_cv2(
128128
video: str,
129129
num_frames: int = 10,
130130
format: str = "pt",
@@ -173,6 +173,43 @@ def load_video(
173173
]
174174

175175

176+
def load_base64_video(video: str) -> BytesIO:
177+
parsed_url = urlparse(video)
178+
data_spec, data = parsed_url.path.split(",", 1)
179+
media_type, data_type = data_spec.split(";", 1)
180+
181+
if data_type != "base64":
182+
msg = "Only base64 data URLs are supported for now."
183+
raise NotImplementedError(msg)
184+
185+
content = base64.b64decode(data)
186+
return content
187+
188+
189+
def load_video(
190+
video: str,
191+
num_frames: int = 10,
192+
format: str = "pt",
193+
device: str = "cpu") -> Union[List[Image.Image], List[torch.Tensor]]:
194+
parsed_url = urlparse(video)
195+
results = None
196+
if parsed_url.scheme in ["http", "https", ""]:
197+
results = _load_video_by_cv2(video, num_frames, format, device)
198+
elif parsed_url.scheme == "data":
199+
decoded_video = load_base64_video(video)
200+
# TODO: any ways to read videos from memory, instead of writing to a tempfile?
201+
with tempfile.NamedTemporaryFile(delete=True,
202+
suffix='.mp4') as tmp_file:
203+
tmp_file.write(decoded_video)
204+
tmp_file.flush()
205+
results = _load_video_by_cv2(tmp_file.name, num_frames, format,
206+
device)
207+
else:
208+
raise ValueError(f"Unsupported video scheme: {parsed_url.scheme}")
209+
210+
return results
211+
212+
176213
async def async_load_video(
177214
video: str,
178215
num_frames: int = 10,
@@ -185,15 +222,24 @@ async def async_load_video(
185222
if parsed_url.scheme in ["http", "https"]:
186223
async with aiohttp.ClientSession() as session:
187224
async with session.get(video) as response:
188-
with tempfile.NamedTemporaryFile(delete=False,
225+
with tempfile.NamedTemporaryFile(delete=True,
189226
suffix='.mp4') as tmp:
190227
tmp.write(await response.content.read())
191-
video_path = tmp.name
192-
# TODO: add case for video encoded in base64
228+
tmp.flush()
229+
results = _load_video_by_cv2(tmp.name, num_frames, format,
230+
device)
231+
elif parsed_url.scheme == "data":
232+
decoded_video = load_base64_video(video)
233+
# TODO: any ways to read videos from memory, instead of writing to a tempfile?
234+
with tempfile.NamedTemporaryFile(delete=True,
235+
suffix='.mp4') as tmp_file:
236+
tmp_file.write(decoded_video)
237+
tmp_file.flush()
238+
results = _load_video_by_cv2(tmp_file.name, num_frames, format,
239+
device)
193240
else:
194-
video_path = video
195-
196-
return load_video(video_path, num_frames, format, device)
241+
results = _load_video_by_cv2(video, num_frames, format, device)
242+
return results
197243

198244

199245
def load_audio(

0 commit comments

Comments
 (0)