Beauvais
Beauvais

Reputation: 2279

Get orientation from camera movie (MOV file) in PHP?

I have uploaded my pictures and movies from my iPhone, to my CentOS web server and now I want to show those on my homepage (I want them hosted locally - not via iCloud). The pictures are no problem, as I can access their data via exif, but it seems not so easy with the movie files.

My server is CentOS with Apache and I am using PHP 8.1 and for now I am planning to only include stuff from my iPhone, but of course it could be expected that I will upload from other cameras also later.

The reason I need the orientation is that I will encapsulate this in to Bootstrap 4.6, to have it automatically being responsive. I will do this:

Landscape movie format (note the 16by9):

<div class='embed-responsive embed-responsive-16by9'>
<video poster='mythumbnail.jpg' controls preload='metadata'>
<source src='mymovie.mov' type='video/mp4' />
<span style='background-color:red;color:white;'>(your browser cannot show this video)</span>
</video>
</div>

Portrait movie format (note the 9by16):

<div class='embed-responsive embed-responsive-9by16'>
<video poster='mythumbnail.jpg' controls preload='metadata'>
<source src='mymovie.mov' type='video/mp4' />
<span style='background-color:red;color:white;'>(your browser cannot show this video)</span>
</video>
</div>

Does anyone have any good ideas on how to get the orientation from a MOV file? :-)

Upvotes: 0

Views: 256

Answers (1)

Beauvais
Beauvais

Reputation: 2279

Until someone provides a better and ideally a native PHP solution, then I will accept this answer based on @CBroe's comment.

PHP does not easily offer a possibility to query video files for meta-data, but a possibility within CentOS (and other Linux distros) is using the mediainfo application, which will be able to query a whole lot of files for its media type and some meta-information. This is true for picture, video and audio files but also e.g. PDF files and others.

To do that from PHP then exec can be used - do also make sure to escape the command argument with escapeshellarg, to make it safe for the shell:

$param = escapeshellarg("myfile.mov"); // make sure the argument is safe to pass
exec("mediainfo $param", $outputArray); // store line-by-line output in an array

The mediainfo will give something like this as output and each line from here will be in the $outputArray array variable:

Array
(
    [0] => General
    [1] => Complete name                            : /myfolder/MVI_0447.mov
    [2] => Format                                   : MPEG-4
    [3] => Format profile                           : QuickTime
    [4] => Codec ID                                 : qt   2007.09 (qt  /CAEP)
    [5] => File size                                : 163 MiB
    [6] => Duration                                 : 1 min 4 s
    [7] => Overall bit rate                         : 21.1 Mb/s
    [8] => Encoded date                             : UTC 2014-08-10 15:32:22
    [9] => Tagged date                              : UTC 2014-08-10 15:32:22
    [10] => CNTH                                     : ?^P??#?k??T???o?2p com.apple.quicktime.make                 : Canon
    [12] => com.apple.quicktime.model                : Canon IXUS 135
    [13] => com.apple.quicktime.rating.user          : 0.000
    [14] => 
    [15] => Video
    [16] => ID                                       : 1
    [17] => Format                                   : AVC
    [18] => Format/Info                              : Advanced Video Codec
    [19] => Format profile                           : Baseline@L4.1
    [20] => Format settings                          : 1 Ref Frames
    [21] => Format settings, CABAC                   : No
    [22] => Format settings, Reference frames        : 1 frame
    [23] => Format settings, GOP                     : M=1, N=12
    [24] => Codec ID                                 : avc1
    [25] => Codec ID/Info                            : Advanced Video Coding
    [26] => Duration                                 : 1 min 4 s
    [27] => Bit rate                                 : 19.5 Mb/s
    [28] => Width                                    : 1 280 pixels
    [29] => Height                                   : 720 pixels
    [30] => Display aspect ratio                     : 16:9
    [31] => Frame rate mode                          : Constant
    [32] => Frame rate                               : 25.000 FPS
    [33] => Color space                              : YUV
    [34] => Chroma subsampling                       : 4:2:0
    [35] => Bit depth                                : 8 bits
    [36] => Scan type                                : Progressive
    [37] => Bits/(Pixel*Frame)                       : 0.848
    [38] => Stream size                              : 151 MiB (93%)
    [39] => Language                                 : English
    [40] => Encoded date                             : UTC 2014-08-10 15:32:22
    [41] => Tagged date                              : UTC 2014-08-10 15:32:22
    [42] => Color range                              : Full
    [43] => Color primaries                          : BT.709
    [44] => Transfer characteristics                 : BT.709
    [45] => Matrix coefficients                      : BT.601
    [46] => Codec configuration box                  : avcC
    [47] => 
    [48] => Audio
    [49] => ID                                       : 2
    [50] => Format                                   : PCM
    [51] => Format settings                          : Little / Signed
    [52] => Codec ID                                 : sowt
    [53] => Duration                                 : 1 min 4 s
    [54] => Bit rate mode                            : Constant
    [55] => Bit rate                                 : 1 536 kb/s
    [56] => Channel(s)                               : 2 channels
    [57] => Channel layout                           : L R
    [58] => Sampling rate                            : 48.0 kHz
    [59] => Bit depth                                : 16 bits
    [60] => Stream size                              : 11.9 MiB (7%)
    [61] => Language                                 : English
    [62] => Encoded date                             : UTC 2014-08-10 15:32:22
    [63] => Tagged date                              : UTC 2014-08-10 15:32:22
    [64] => 
    [65] => 
)

This is then for you to interpret - and to handle the numerous possibilities for bad meta-data, when using a variety of camera sources or medias :-) E.g. for picture files I am only using mediainfo to determine that it is a picture file and then I pass the data via exif_read_data as this is much more detailed.

One minor note though - for my particular setup it seems that exec has some problems with Danish national characters, as it cannot read files with these characters inside. I expect this to be something related to some character encoding issue, but never figured that out - view PHP "exec" cannot access filenames which have national Danish characters? for details.

Upvotes: 1

Related Questions