Skip to content
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

FPS gets rounded to two decimals after the point #116

Open
isarandi opened this issue Jun 26, 2024 · 3 comments
Open

FPS gets rounded to two decimals after the point #116

isarandi opened this issue Jun 26, 2024 · 3 comments

Comments

@isarandi
Copy link

isarandi commented Jun 26, 2024

https://github.com/imageio/imageio-ffmpeg/blob/0585a7b5fa0ea0628712b319ff898ad6f537b03e/imageio_ffmpeg/_io.py#L528C4-L528C65

Here, the fps gets rounded, which makes it hard to transform an existing video's frames while keeping the source fps in the resulting video (so that it e.g. doesn't lose sync when you add audio to it or subtitles or anything else).

Even the metadata['fps'] is rounded when checking the fps of an existing video since it gets parsed from ffmpeg output header.

My workaround for getting the fps is:

def get_fps(video_path):
    command = [
        'ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=r_frame_rate',
        '-of', 'json', video_path]
    try:
        output = subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True)
        frame_rate = json.loads(output)['streams'][0]['r_frame_rate']
        numerator, denominator = map(int, frame_rate.split('/'))
        return numerator / denominator
    except subprocess.CalledProcessError as e:
        raise ValueError(f"Failed to retrieve FPS: {e.output}")
    except KeyError as e:
        raise ValueError(f"Key error: {e}")

And the workaround for specifying the fps is:

imageio.get_writer(path, input_params=['-r', str(fps)])

or

from fractions import Fraction
fps_frac = Fraction(fps).limit_denominator()
imageio.get_writer(path, input_params=['-r', str(fps_frac)])

But it would be best if imageio handled exact frame rates out of the box without rounding.

@almarklein
Copy link
Member

Good point. A PR is very welcome!

@isarandi
Copy link
Author

The output fps could indeed be fixed, but I think for reading the exact fps, ffprobe is needed (the ffmpeg binary only prints the fps rounded to two decimals), but apparently that dependency is not part of this library, so it can't be done.

@almarklein
Copy link
Member

Mmm, good point...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants