Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#26415·ultralytics

save=True with vid_stride > 1 writes the video at the source fps, so it plays vid_stride times faster than the source

Author: Nicholas022400701Created Sep 28, 2026Updated Sep 28, 2026
Labelsbug

Search before asking

  • I have searched the Ultralytics YOLO issues and found no similar bug report.

Ultralytics YOLO Component

Predict

Bug

BasePredictor.save_predicted_images opens the cv2.VideoWriter with the source frame rate (fps = self.dataset.fps if self.dataset.mode == "video" else 30, ultralytics/engine/predictor.py line 536) while vid_stride makes the loader hand over only every Nth frame. The saved video therefore has 1/vid_stride of the frames at the full frame rate and plays vid_stride times faster than the source: a 2.0 s clip predicted with save=True, vid_stride=3 comes back as a 0.67 s clip. The same happens with model.track(..., save=True, vid_stride=N), where the sped-up video is also what most people would use to check the tracks.

The strongest reading of the current behaviour I can see is that vid_stride is a speed knob and the saved file simply contains the processed frames, so a shorter time-lapse is what you get. Against that: vid_stride is documented as "skipping frames in videos to speed up processing at the cost of temporal resolution", not as changing the output timeline, nothing warns that the saved video runs fast, and a saved video that no longer lines up with the source clock is hard to compare with the source or with the original audio track. Dividing the writer's frame rate by vid_stride keeps the source duration and does not change anything for vid_stride=1.

Fix: fps = max(1, round(fps / self.args.vid_stride)) after that line, for both videos and streams (streams are written at a fixed 30 fps and are skipped the same way). The writer takes an integer frame rate, so 30 fps with vid_stride=4 is written at 8 fps instead of 7.5 (7% fast) rather than 30 (300% fast). A test with a 12-frame 30 fps clip and vid_stride=3 checks 4 frames at 10 fps. Fix and test are ready, see the patch below. Happy to open the PR if you agree this should keep the source duration.

Environment

Ultralytics 8.4.164 (main 4604393) Python-3.13.14 torch-2.14.0+cpu CPU (Intel Xeon Processor @ 2.90GHz)
OS: Linux (Amazon Linux 2023), OpenCV 4.12.0

Minimal Reproducible Example

python
import glob, shutil, tempfile
import cv2, numpy as np
from ultralytics import YOLO

tmp = tempfile.mkdtemp()
src = f"{tmp}/src30.avi"  # 60 grey frames at 30 fps, 2.0 s
writer = cv2.VideoWriter(src, cv2.VideoWriter_fourcc(*"MJPG"), 30, (320, 240))
for i in range(60):
    writer.write(np.full((240, 320, 3), 128, np.uint8))
writer.release()

model = YOLO("yolo26n.pt")
for vid_stride in (1, 3):
    shutil.rmtree(f"{tmp}/out", ignore_errors=True)
    results = model.predict(src, vid_stride=vid_stride, save=True, project=tmp, name="out", verbose=False)
    cap = cv2.VideoCapture(glob.glob(f"{tmp}/out/src30.*")[0])  # .avi on Linux and Windows, .mp4 on macOS
    frames, fps = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), cap.get(cv2.CAP_PROP_FPS)
    print(f"vid_stride={vid_stride}: {len(results)} frames processed, saved {frames} frames at {fps:g} fps = {frames / fps:.2f} s (source 2.00 s)")

Output on main:

vid_stride=1: 60 frames processed, saved 60 frames at 30 fps = 2.00 s (source 2.00 s)
vid_stride=3: 20 frames processed, saved 20 frames at 30 fps = 0.67 s (source 2.00 s)

With the patch:

vid_stride=1: 60 frames processed, saved 60 frames at 30 fps = 2.00 s (source 2.00 s)
vid_stride=3: 20 frames processed, saved 20 frames at 10 fps = 2.00 s (source 2.00 s)

Additional

The test fails on main (got 4 at 30.0) and passes with the fix; pytest tests/test_python.py -k "predict_txt or predict_csv or vid_stride or track_second" gives 7 passed, ruff format --check and ruff check are clean for the changed files. Branch: https://github.com/Nicholas022400701/ultralytics/tree/fix/save-video-fps-vid-stride

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!

Patch

git diff
diff
diff --git a/tests/test_python.py b/tests/test_python.py
index 82339f4..dab878b 100644
--- a/tests/test_python.py
+++ b/tests/test_python.py
@@ -382,6 +382,21 @@ def test_model_profile():
     _ = model.predict(im, profile=True)
 
 
+def test_predict_save_video_vid_stride_keeps_duration(tmp_path):
+    """The saved video keeps the source duration when vid_stride skips frames instead of playing vid_stride times faster."""
+    src = tmp_path / "src.avi"
+    writer = cv2.VideoWriter(str(src), cv2.VideoWriter_fourcc(*"MJPG"), 30, (64, 48))
+    for _ in range(12):
+        writer.write(np.zeros((48, 64, 3), dtype=np.uint8))
+    writer.release()
+    YOLO(MODEL).predict(str(src), save=True, vid_stride=3, imgsz=32, project=tmp_path, name="out", verbose=False)
+    out = next(p for p in (tmp_path / "out").iterdir() if p.suffix in {".avi", ".mp4"})
+    cap = cv2.VideoCapture(str(out))
+    frames, fps = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), cap.get(cv2.CAP_PROP_FPS)
+    cap.release()
+    assert (frames, fps) == (4, 10), f"expected 4 frames at 10 fps from 12 frames at 30 fps, got {frames} at {fps}"
+
+
 def test_predict_txt(tmp_path):
     """Test YOLO predictions with file, directory, and pattern sources listed in a text file."""
     file = tmp_path / "sources_multi_row.txt"
diff --git a/ultralytics/engine/predictor.py b/ultralytics/engine/predictor.py
index b253d0d..d6f299d 100644
--- a/ultralytics/engine/predictor.py
+++ b/ultralytics/engine/predictor.py
@@ -534,6 +534,7 @@ class BasePredictor:
         # Save videos and streams
         if self.dataset.mode in {"stream", "video"}:
             fps = self.dataset.fps if self.dataset.mode == "video" else 30
+            fps = max(1, round(fps / self.args.vid_stride))  # skipped frames must not shorten the saved video
             frames_path = self.save_dir / f"{save_path.stem}_frames"  # save frames to a separate directory
             if save_path not in self.vid_writer:  # new video
                 if self.args.save_frames:

Source: ultralytics/ultralytics

View original on GitHubView discussion on GitHub