Make WordPress Core

Opened 20 months ago

Last modified 16 months ago

#56217 new enhancement

Add video rotation info in wp_read_video_metadata()

Reported by: opajaap's profile opajaap Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.0
Component: Media Keywords: has-unit-tests has-patch
Focuses: Cc:

Description

wp_read_video_metadata() does not include ‘rotate’ in its return array. It would be nice to add it so the server can detect that width and height should be swapped in case of a portrait oriented video from a smartphone.

Just adding:

   if ( ! empty( $data['video']['rotate'] ) ) {
		$metadata['rotate'] = $data['video']['rotate'];
   }

right after:

   if ( ! empty( $data['audio'] ) ) {
        unset( $data['audio']['streams'] );
        $metadata['audio'] = $data['audio'];
   }

will do.

Attachments (7)

VID-20190901-WA0000.mp4 (3.5 MB) - added by opajaap 20 months ago.
sample portrait video
wrong sizes.png (33.0 KB) - added by opajaap 20 months ago.
Screenshot wrong sizes
Rigt sizes.png (85.6 KB) - added by opajaap 20 months ago.
Right sizes
Extract_video_rotation_meta_info_if_available.patch (716 bytes) - added by martin.krcho 20 months ago.
56217.2.patch (1.8 KB) - added by martin.krcho 20 months ago.
Extract_video_rotation_meta_info_if_available.patch along with unit tests.
rotated-video-180.mov (198.4 KB) - added by martin.krcho 20 months ago.
rotated-video-270.mov (224.4 KB) - added by martin.krcho 20 months ago.

Change History (17)

#1 @martin.krcho
20 months ago

  • Keywords needs-testing added
  • Summary changed from Missing item in wp_read_video_metadata() to Add video rotation info in wp_read_video_metadata()

Thank you for your ticket, @opajaap.

Your suggestion sounds very reasonable. I checked the getids3 library and I can confirm that the rotate meta info is supported for the QuickTime video at the moment.
https://github.com/JamesHeinrich/getID3/blob/master/getid3/module.audio-video.quicktime.php#L1400-L1402

Would you be able to provide a sample video and some instructions for testings this? Calling the function wp_read_video_metadata() and checking the response would obviously be the easiest check :) However, do the height and width appear swapped somewhere in the UI? Is the video displayed incorrectly after upload?

#2 @opajaap
20 months ago

I need this addition for my plugin wp-photo-album-plus

I made a copy of wp_read_video_metadata(), named it wppa_read_video_metadata() and added the proposed addition.

If you use the the current release candidate of my plugin (without the addition):
https://downloads.wordpress.org/plugin/wp-photo-album-plus.8.2.03.006.zip

You get the wrong sizes,
when you use the dev version (with the addition):
https://downloads.wordpress.org/plugin/wp-photo-album-plus.8.2.04.001.zip

You get the right sizes, because of this piece of code in my function wppa_fix_video_metadata():

	$mp4info = wppa_read_video_metadata( $file );

	// Make sure its a video
	if ( $mp4info['fileformat'] != 'mp4' ) {
		wppa_log( 'dbg', 'No mp4 fileformat in ' . $file . ' ' . $where );
		return false;
	}

	// Find sizes
	$videox = isset( $mp4info['width'] ) ? $mp4info['width'] : '0';
	$videoy = isset( $mp4info['height'] ) ? $mp4info['height'] : '0';
	
	// Rotated?
	if ( isset( $mp4info['rotate'] ) ) {
		$rot = $mp4info['rotate'];
		if ( $rot == 90 || $rot == 270 ) {
			$t = $videox;
			$videox = $videoy;
			$videoy = $t;
		}
	}

@opajaap
20 months ago

sample portrait video

@opajaap
20 months ago

Screenshot wrong sizes

@opajaap
20 months ago

Right sizes

#3 @martin.krcho
20 months ago

  • Keywords needs-unit-tests added; needs-testing removed

Thank you for the sample video and also the testing scenario.

I can confirm that this ticket is valid and reproducible.

The attached patch fixes the problem. I started working on the unit tests. I am not sure we can use your video for the unit tests (for various reasons). I will try to create some sample videos for the tests that are also small in size.

@martin.krcho
20 months ago

Extract_video_rotation_meta_info_if_available.patch along with unit tests.

#4 @martin.krcho
20 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

56217.2.patch contains the fix along with unit tests.

The attached videos rotated-video-180.mov and rotated-video-270.mov must be placed in tests/phpunit/data/uploads.

#5 @Presskopp
19 months ago

  • Keywords needs-patch added

#6 @Presskopp
19 months ago

  • Keywords has-patch added; needs-patch removed

This ticket was mentioned in PR #3101 on WordPress/wordpress-develop by martinkrcho.


19 months ago
#7

This PR improves function wp_read_video_metadata() to include video rotation in the video metadata if it's available.

Trac ticket: https://core.trac.wordpress.org/ticket/56217

This ticket was mentioned in Slack in #core by martin.krcho. View the logs.


18 months ago

#9 @opajaap
16 months ago

Still not fixed in 6.1-RC5

Does it take over three months to add this simple harmless foolproof code?:

<?php
// Rotated?
        if ( isset( $mp4info['rotate'] ) ) {
                $rot = $mp4info['rotate'];
                if ( $rot == 90 || $rot == 270 ) {
                        $t = $videox;
                        $videox = $videoy;
                        $videoy = $t;
                }
        }

#10 @opajaap
16 months ago

Correction: only rquired:

if ( ! empty( $data['video']['rotate'] ) ) {
                $metadata['rotate'] = $data['video']['rotate'];
        }
Note: See TracTickets for help on using tickets.