64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
import re
|
||
from datetime import datetime
|
||
from typing import Dict, Tuple
|
||
|
||
# 你 COS 的公共前缀可以做成默认参数,便于不同环境覆盖
|
||
DEFAULT_COS_PREFIX = "https://data-miningc01-1318950322.cos.ap-shanghai-adc.myqcloud.com/"
|
||
|
||
def extract_datetime_from_filename(
|
||
bag_filename: str,
|
||
cos_prefix: str = DEFAULT_COS_PREFIX,
|
||
) -> Tuple[str, Dict[str, str]]:
|
||
"""
|
||
从 .bag 文件名中提取采集时间,并生成对应的多视角视频 URL。
|
||
|
||
期望文件名包含片段:_{YYYYMMDD}-{HHMMSS}_ 或以 .bag 结尾,例如:
|
||
PL162802_event_all_time_event_20250416-193015_0.bag
|
||
PL123456_event_all_time_event_20250828-221018.bag
|
||
xx_20250524-180926_.bag
|
||
xx_20250524-180926_3.bag
|
||
|
||
返回:
|
||
(datetime_full_str, video_urls)
|
||
- datetime_full_str: 'YYYY-MM-DD HH:MM:SS'
|
||
- video_urls: COS 上该 bag 的多视角视频 URL 字典
|
||
|
||
异常:
|
||
ValueError: 当文件名不匹配预期格式时抛出
|
||
"""
|
||
# 提取日期时间
|
||
match = re.search(r"_(\d{8})-(\d{6})(?:_|\.|$)", bag_filename)
|
||
|
||
if not match:
|
||
raise ValueError(f"文件名格式错误,无法提取时间信息:{bag_filename}")
|
||
|
||
date_part, time_part = match.groups()
|
||
year, month, day = date_part[:4], date_part[4:6], date_part[6:8]
|
||
hour, minute, second = time_part[:2], time_part[2:4], time_part[4:6]
|
||
|
||
datetime_full = f"{year}-{month}-{day} {hour}:{minute}:{second}"
|
||
|
||
|
||
print(f'datetime_full:{datetime_full}')
|
||
# 生成视频路径(与现有逻辑一致)
|
||
# 去掉 .bag 后缀,再补上 " - {View}.mp4"
|
||
base = bag_filename[:-4] if bag_filename.endswith(".bag") else bag_filename
|
||
view_map = {
|
||
"wide": "Wide",
|
||
"left": "Left",
|
||
"right": "Right",
|
||
"rear_left": "Rear Left",
|
||
"rear_right": "Rear Right",
|
||
"rear": "Rear",
|
||
}
|
||
video_urls = {}
|
||
for key, label in view_map.items():
|
||
video_final_path = f"{base}- {label}.mp4"
|
||
# 目录层级:momenta/videos/YYYY/MM/DD/{文件名 - {View}.mp4}
|
||
video_key = f"momenta/videos/{year}/{month}/{day}/{video_final_path}"
|
||
video_urls[key] = f"{cos_prefix}{video_key}"
|
||
|
||
print(f'v{video_urls}')
|
||
|
||
return datetime_full, video_urls
|