mydia icon indicating copy to clipboard operation
mydia copied to clipboard

mkv video files do not have "nb_frames" field in video header

Open Infinoid opened this issue 4 years ago • 2 comments

I tried reading a .mkv file and it crashed.

    video = mydia.Videos().read(video_path)
  File ".../python3.6/site-packages/mydia/mydia.py", line 234, in read
    video_tensor = np.vstack(map(self._read_video, paths_iterator))
  File "<__array_function__ internals>", line 6, in vstack
  File ".../python3.6/site-packages/numpy/core/shape_base.py", line 280, in vstack
    arrs = atleast_2d(*tup)
  File ".../python3.6/site-packages/mydia/mydia.py", line 286, in _read_video
    fps, total_frames = self._probe(path)
  File ".../python3.6/site-packages/mydia/mydia.py", line 365, in _probe
    total_frames = int(video_stream["nb_frames"])
KeyError: 'nb_frames'

To reproduce:

  1. Locate a video file which works
  2. Convert it to .mkv as follows: ffmpeg -i working.mp4 -vc copy test.mkv
  3. Try to read mkv file

I printed the video_stream data in _probe(), here was the result:

{'index': 0, 'codec_name': 'h264', 'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10', 'profile': 'High', 'codec_type': 'video', 'codec_time_base': '1/50', 'codec_tag_string': '[0][0][0][0]', 'codec_tag': '0x0000', 'width': 700, 'height': 500, 'coded_width': 700, 'coded_height': 500, 'has_b_frames': 2, 'sample_aspect_ratio': '0:1', 'display_aspect_ratio': '0:1', 'pix_fmt': 'yuv420p', 'level': 30, 'chroma_location': 'left', 'field_order': 'progressive', 'refs': 1, 'is_avc': 'true', 'nal_length_size': '4', 'r_frame_rate': '25/1', 'avg_frame_rate': '25/1', 'time_base': '1/1000', 'start_pts': 0, 'start_time': '0.000000', 'bits_per_raw_sample': '8', 'disposition': {'default': 1, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'HANDLER_NAME': 'VideoHandler', 'ENCODER': 'Lavc57.107.100 libx264', 'DURATION': '00:00:05.120000000'}}

Do .mp4 files work reliably? If so, converting input to .mp4 should work as a workaround.

Infinoid avatar Mar 31 '20 01:03 Infinoid

Hi @Infinoid

Right now, it'll be difficult for me to test (and possibly fix) the issue. So, I would recommend using .mp4 for now. Let me know if even then you face a similar problem.

I'll update here as soon as I get time to look into this.

Also, is there any particular reason why you're converting videos to .mkv?

MrinalJain17 avatar Apr 05 '20 10:04 MrinalJain17

The input files I received are in .mkv format and I was hoping to read from them as-is.

FWIW, it looks like scikit-video works around this by running ffprobe -count_frames.

Infinoid avatar Apr 06 '20 02:04 Infinoid