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
Bug:
read_frames
read 2 frames as one fromCode
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 defaultread_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
takebpp
as int, so we can't pass 1.5, so we can addbits_per_pixel
and don't removebpp
for backward compatibilityRelated issue: #24