imageio / imageio-ffmpeg

FFMPEG wrapper for Python
BSD 2-Clause "Simplified" License
221 stars 50 forks source link

added bits_per_pixel parameter to read_frames #25

Closed mastak closed 4 years ago

mastak commented 4 years ago

Bug: read_frames read 2 frames as one from

Code

import os
import tempfile
from urllib.request import urlopen

import imageio_ffmpeg

url = "https://raw.githubusercontent.com/imageio/imageio-binaries/master/images/realshort.mp4"
content = urlopen(url).read()
file_path = os.path.join(tempfile.gettempdir(), "cockatoo.mp4")
with open(file_path, "wb") as f:
    f.write(content)

gen = imageio_ffmpeg.read_frames(file_path, pix_fmt="yuv420p")

meta = gen.__next__()
frames_count_meta = meta['fps'] * meta['duration']
frames = list(gen)
print(f'{frames_count_meta} vs {len(frames)}')

Expected

36.024 vs 36

Result

36.024 vs 18

Cause

read_frames can handle only bytes per pixel, but yuv 420 has 6 bytes per 4 pixels (12 bits per 1 pixel). By default read_frames read 3 bytes per pixel, but because yuv420 has 2 pixel in 3 bytes we have 18 frames instead of 36.

Solutions

read_frames take bpp as int, so we can't pass 1.5, so we can add bits_per_pixel and don't remove bpp for backward compatibility

Related issue: #24

almarklein commented 4 years ago

Thanks!