Skip to content

Fix: PyAV does not support floating point numbers with decimals as FPS when writing and will throw in case this constraint is not satisfied. #2334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions torchvision/io/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import math
import re
import warnings
from typing import Tuple, List
from typing import List, Tuple, Union

import numpy as np
import torch
Expand Down Expand Up @@ -49,7 +49,7 @@ def _av_available():
_GC_COLLECTION_INTERVAL = 10


def write_video(filename, video_array, fps, video_codec="libx264", options=None):
def write_video(filename, video_array, fps: Union[int, float], video_codec="libx264", options=None):
"""
Writes a 4d tensor in [T, H, W, C] format in a video file

Expand All @@ -65,6 +65,11 @@ def write_video(filename, video_array, fps, video_codec="libx264", options=None)
_check_av_available()
video_array = torch.as_tensor(video_array, dtype=torch.uint8).numpy()

# PyAV does not support floating point numbers with decimal point
# and will throw OverflowException in case this is not the case
if isinstance(fps, float):
fps = np.round(fps)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we would prefer to use native python functions over numpy functions (in this case round from python).
The reason being that numpy can't be torchscripted, while many python builtins can.
In here this doesn't make a difference because this function can't be torchscripted anyway (due to pyav), but this is something good to keep in mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thank you for the comment ! This makes totally sense: I can replace this with round.

Regarding the fix itself: the issue is more a problem of symmetry:

  • reading with read_video will return me a floating point number
  • writing with this floating point number will not work (and the error is not clear)

The net effect is that if one tries to read a video to write it afterwards (for instance, to resize it), the error will be very hard to understand. My proposal is not necessarily the best way to fix this though: we could at least add a warning. Or we could decide to try something more ambitious.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a valid point.

The original reason why I added write_video was purely for testing purposes (to validate that video reading was working), so I didn't really think much about it (maybe I should have made it private?). But looks like more people are relying on it / using it.

The decision between using ceil / floor / round is not yet very clear to me yet. round seems ok, so let's go for it. But given that videos can have a varying fps, the concept of a single fps is not really well-defined.


container = av.open(filename, mode="w")

stream = container.add_stream(video_codec, rate=fps)
Expand Down