1

I have a bunch of videos from an iPhone and am trying to get their width and height programmatically using opencv. Here is my code:

import os, glob
import cv2
ff = glob.glob("*.m??") + glob.glob("*.M??")
for f in ff:
    cap = cv2.VideoCapture(f)
    w, h = (int(cap.get(i)) for i in (3, 4))
    cap.release()
    print("%s: %dx%d" % (f, w, h))

The problem is, the result is always the same: 1920x1080, -- regardless of whether the movies is horizontal (1920*1080) or vertical (I would expect 1080*1920).

Any ideas how to fix this? Maybe there is a flag somewhere that tells if it's Horiz or Vert? I didn't find anything here at opencv docs... Thank you!

Vlad K.
  • 300
  • 3
  • 11
  • 1
    the camera always records in horizontal mode, you'll have to read the "rotation information" of the device/video and rotate the video manually. Maybe OpenCV does not have options to read rotation information. – Micka Sep 01 '18 at 16:27
  • Thank you Micka, but as I wrote I am looking for exactly that – some sort of a flag... There is nothing that seems relevant to me here: https://docs.opencv.org/3.3.1/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d – Vlad K. Sep 01 '18 at 16:47
  • Oh, I forgot to mention I am working with *.MOV videos. – Vlad K. Sep 01 '18 at 16:47
  • BTW: after I convert the video from MOV to mp4, the library reports correctly the dimensions of *mp4 (i.e. w > h for horizontal and w < h for vertical). – Vlad K. Sep 01 '18 at 16:49
  • how did you convert? Probably the converter alpplication did rotate the images. – Micka Sep 01 '18 at 17:07
  • I used ffmpeg (stand–alone binary cmd line), I didn't give it any dimensions as parameters, it "figured it out" somehow. Note: this does not solve my problem, I need to know the dimensions before conversion. – Vlad K. Sep 01 '18 at 18:37
  • 2
    I think it is not possible to get orientation with opencv - you can use ffmpeg to get orientation data (see [how-to-extract-orientation-information-from-videos](https://stackoverflow.com/questions/5287603/how-to-extract-orientation-information-from-videos/14237677#14237677)) – Dmitrii Z. Sep 01 '18 at 18:50
  • 1
    Dmitrii Z: Hah! :) It's called a KLUDGE but definitely works for me, thank you!!! – Vlad K. Sep 01 '18 at 20:02
  • Off-topic: I accidentally clicked twice on Dmitrii Z's comment's up–arrow, and now I cannot fix it, receiving: "You've already undone your vote on this comment, you cannot upvote it again". Any suggestions? – Vlad K. Sep 01 '18 at 20:13
  • @VladK. Yeah, don't worry about it, just keep it in mind for the future ;) | BTW, if you have a working solution, then by all means post it as a self-answer. – Dan Mašek Sep 01 '18 at 22:15

1 Answers1

0

Apparently all I needed to do is install the latest ffmpeg (which took some effort:).

ffmpeg -i <flnm>

outputs, among other things, a line with "rotate" in it, like this:

Metadata:
  rotate          : 90
  creation_time   : 2015-12-09T00:28:42.000000Z
  handler_name    : Core Media Data Handler
  encoder         : H.264

I found four cases: values 90, 180, 270 for rotate, or a line with 'rotate' is absent (== no rotation). To my needs, 90 and 270 correspond to "vertical" (.a.k.a. "portrait"), so my problem is solved.

Parsing is trivial, I don't think anybody wants my code here. Just a small note: you need to catch stderr rather than stdout from ffmeg.

Thanks all!

Vlad K.
  • 300
  • 3
  • 11