Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -195,29 +195,71 @@ except ImportError:
|
|
| 195 |
print("[warn] mcap / mcap_protobuf not installed; abc-teleop conversion disabled.")
|
| 196 |
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
def convert_mcap_camera(mcap_path: str, topic: str, tmp_dir: str, label: str) -> str:
|
| 199 |
-
"""Extract one camera topic from mcap and encode to mp4. Returns output path."""
|
| 200 |
temp_h264 = os.path.join(tmp_dir, f"{label}.h264")
|
| 201 |
output = os.path.join(tmp_dir, f"{label}.mp4")
|
| 202 |
|
|
|
|
| 203 |
with open(temp_h264, "wb") as out:
|
| 204 |
with open(mcap_path, "rb") as f:
|
| 205 |
reader = make_reader(f, decoder_factories=[DecoderFactory()])
|
| 206 |
for schema, channel, msg, decoded in reader.iter_decoded_messages():
|
| 207 |
if channel.topic != topic:
|
| 208 |
continue
|
|
|
|
| 209 |
out.write(decoded.data)
|
| 210 |
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
["ffmpeg", "-y",
|
| 213 |
-
"-
|
|
|
|
| 214 |
"-i", temp_h264,
|
| 215 |
"-c:v", "libx264", "-preset", "fast",
|
| 216 |
"-crf", "18", "-pix_fmt", "yuv420p",
|
| 217 |
"-movflags", "+faststart",
|
| 218 |
output],
|
| 219 |
-
|
| 220 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
os.remove(temp_h264)
|
| 222 |
return output
|
| 223 |
|
|
|
|
| 195 |
print("[warn] mcap / mcap_protobuf not installed; abc-teleop conversion disabled.")
|
| 196 |
|
| 197 |
|
| 198 |
+
# def convert_mcap_camera(mcap_path: str, topic: str, tmp_dir: str, label: str) -> str:
|
| 199 |
+
# """Extract one camera topic from mcap and encode to mp4. Returns output path."""
|
| 200 |
+
# temp_h264 = os.path.join(tmp_dir, f"{label}.h264")
|
| 201 |
+
# output = os.path.join(tmp_dir, f"{label}.mp4")
|
| 202 |
+
|
| 203 |
+
# with open(temp_h264, "wb") as out:
|
| 204 |
+
# with open(mcap_path, "rb") as f:
|
| 205 |
+
# reader = make_reader(f, decoder_factories=[DecoderFactory()])
|
| 206 |
+
# for schema, channel, msg, decoded in reader.iter_decoded_messages():
|
| 207 |
+
# if channel.topic != topic:
|
| 208 |
+
# continue
|
| 209 |
+
# out.write(decoded.data)
|
| 210 |
+
|
| 211 |
+
# subprocess.run(
|
| 212 |
+
# ["ffmpeg", "-y",
|
| 213 |
+
# "-framerate", str(FPS),
|
| 214 |
+
# "-i", temp_h264,
|
| 215 |
+
# "-c:v", "libx264", "-preset", "fast",
|
| 216 |
+
# "-crf", "18", "-pix_fmt", "yuv420p",
|
| 217 |
+
# "-movflags", "+faststart",
|
| 218 |
+
# output],
|
| 219 |
+
# check=True, capture_output=True,
|
| 220 |
+
# )
|
| 221 |
+
# os.remove(temp_h264)
|
| 222 |
+
# return output
|
| 223 |
+
|
| 224 |
def convert_mcap_camera(mcap_path: str, topic: str, tmp_dir: str, label: str) -> str:
|
|
|
|
| 225 |
temp_h264 = os.path.join(tmp_dir, f"{label}.h264")
|
| 226 |
output = os.path.join(tmp_dir, f"{label}.mp4")
|
| 227 |
|
| 228 |
+
timestamps = []
|
| 229 |
with open(temp_h264, "wb") as out:
|
| 230 |
with open(mcap_path, "rb") as f:
|
| 231 |
reader = make_reader(f, decoder_factories=[DecoderFactory()])
|
| 232 |
for schema, channel, msg, decoded in reader.iter_decoded_messages():
|
| 233 |
if channel.topic != topic:
|
| 234 |
continue
|
| 235 |
+
timestamps.append(msg.log_time) # nanoseconds
|
| 236 |
out.write(decoded.data)
|
| 237 |
|
| 238 |
+
# derive fps from median inter-frame interval
|
| 239 |
+
if len(timestamps) >= 2:
|
| 240 |
+
intervals = [timestamps[i+1] - timestamps[i] for i in range(len(timestamps) - 1)]
|
| 241 |
+
median_ns = sorted(intervals)[len(intervals) // 2]
|
| 242 |
+
fps = round(1e9 / median_ns) if median_ns > 0 else FPS
|
| 243 |
+
else:
|
| 244 |
+
fps = FPS
|
| 245 |
+
|
| 246 |
+
print(f"[{label}] detected {fps} fps from {len(timestamps)} frames")
|
| 247 |
+
|
| 248 |
+
result = subprocess.run(
|
| 249 |
["ffmpeg", "-y",
|
| 250 |
+
"-err_detect", "ignore_err",
|
| 251 |
+
"-framerate", str(fps),
|
| 252 |
"-i", temp_h264,
|
| 253 |
"-c:v", "libx264", "-preset", "fast",
|
| 254 |
"-crf", "18", "-pix_fmt", "yuv420p",
|
| 255 |
"-movflags", "+faststart",
|
| 256 |
output],
|
| 257 |
+
capture_output=True, text=True,
|
| 258 |
)
|
| 259 |
+
if result.returncode != 0:
|
| 260 |
+
print(f"[ffmpeg stderr] {result.stderr[-2000:]}")
|
| 261 |
+
raise RuntimeError(f"ffmpeg failed (exit {result.returncode})")
|
| 262 |
+
|
| 263 |
os.remove(temp_h264)
|
| 264 |
return output
|
| 265 |
|