[IOS] Some questions about video direction

Source: Internet
Author: User

Version: OS X 10.10.5 Xcode 6.4 (6e35b) IOS >= 7 I. Rotation metadata in mov/mp4 video filesThe Mov/mp4 video recorded on iOS built-in camera app may produce a rotation metadata that indicates how many angles the camera rotated to when the video was recorded. The values are typically four: 0, 90, 180, or 270. Orientation metadata similar to the EXIF information in a picture file. Rotation metadata is used by the player to determine the direction of the rendered video, but some players ignore it. Several common player/playback controls are tested to support rotation metadata later.
Note: The rotation metadata of a video file is actually not a saved angle value, but it is easy to understand if you are only concerned with angle issues rather than image stretching. about how to get rotation metadata angle value, interested can see the source of VLC.
Here's a look at the two videos recorded with the rear camera using the iphone camera app to see its rotation meta-data with MediaInfo. Please note that the filenames are Img_1427.mov and Img_1428.mov, which will be compared with the two files. 1, using the rear-facing camera in the portrait (vertical screen, home key in the bottom) mode recording video, its rotation value is 90. (Figure A: Rotation value is 90) 2, using the rear camera in landscaperigth (horizontal screen, home key on the right) mode when the video recorded, there is no rotation metadata, or the rotation value is 0. (Fig. II: No rotation value or rotation value of 0) the four angle values of the 0, 90, 180, and 270 of the rotation can be interpreted this way: Landscaperigth is 0 degrees, the home key or the camera is the center,   Rotate clockwise to portrait to 90 degrees, rotate to landscapeleft to 180 degrees, and rotate to Portraitupsidedown to 270 degrees. Here first look at the properties of these two video files on OS X 10.10.4 and Windows 8:1. Export the video file from the phone to OS X and preview in the Finder, and the two files are displayed in the correct direction. Then view the properties of the Img_1427.mov video file with a rotation value of 90. Displays its size as 1080*1920, not 1920*1080. But don't be deceived by this illusion, the actual size of the video is still 1920*1080. Finally see no rotation value or say rotation value is 0 Img_1428.mov video file, show its size is 1920*1080, everything is normal.   With QuickTime playback, you can correctly identify the direction of the two videos. (figure III) 2, preview in Windows Explorer, Img_1427.mov and Img_1428.mov are displayed in the correct direction, and then view the properties of the two files, the dimensions are displayed as 1920*1080, and the Windows Media Player is used to Can correctly identify the direction of the two video. second, the common video player to identify the directionWindows Media Player on the player and Win8 on the iOS album is able to correctly identify the direction of the Mov/mp4, that is, Img_1427.mov video with an actual size of 1920*1080 and a rotation value of 90 can press 1080* 1920 size and adjust direction for rendering, no rotation value, or rotation value of 0 Img_1428.mov video is rendered in 1920*1080 size and in the actual direction. A similar situation exists in andriod. VLC for OS X (why?) and iOS Mpmovieplayerviewcontrolle are not recognized for rotation, they are always rendered in actual size and default orientation. For Mpmovieplayerviewcontrolle there is a solution below. The player that the Safari browser brings up should also be mpmovieplayerviewcontroller, so the orientation is not recognized correctly. three, Mpmovieplayerviewcontroller control video DirectionAdditional parameters are required to determine the orientation of the video and then rotate the player to reach various video--mov/mp4/m3u8, etc.-all of which can be played correctly.
......    NSString * url = @ "http://www.yourdomain.com/Videos/1.m3u8";    Mpmovieplayerviewcontroller * VC = [[Mpmovieplayerviewcontroller alloc] init];    Vc.moviePlayer.contentURL = [Nsurl Urlwithstring:url];    Here is a video of rotation 90, the home key recorded under the video [self ROTATEVIDEOVIEW:VC degrees:90];    [Self PRESENTMOVIEPLAYERVIEWCONTROLLERANIMATED:VC]; [Vc.movieplayer play];    ...-(void) Rotatevideoview: (Mpmovieplayerviewcontroller *) Moveplayerviewcontroller degrees: (Nsinteger) degrees{ if (degrees==0| |    DEGREES==360) return;    if (degrees<0) degrees = (degrees% 360) + 360;    if (degrees>360) degrees = degrees% 360; Mpvideoview in the iOS8 tag is 1002, do not rule out the possibility of Apple later change. Refer to recursively view the LLDB command for the View hierarchy: (LLDB) PO [moveplayerviewcontroller.view recursivedescription] UIView *videoview = [Moveplayerv    Iewcontroller.view viewwithtag:1002]; if ([Videoview iskindofclass:nsclassfromstring (@ "Mpvideoview")]) {videoview.transform = Cgaffinetransformmakerotat        Ion (M_PI * degrees/180.0); VideovIew.frame = MovePlayerViewController.view.bounds; }}

Change to Category:

#import "Mpmovieplayerviewcontroller+rotation.h" @implementation Mpmovieplayerviewcontroller (Rotation)-(void) Rotatevideoviewwithdegrees: (nsinteger) degrees{    if (degrees==0| | degrees==360) return;    if (degrees<0) degrees = (degrees%) +;    if (degrees>360) degrees = degrees%;       Mpvideoview in the iOS8 tag is 1002, do not rule out the possibility of Apple later change. Refer to recursively view the LLDB command for the View hierarchy: (LLDB) PO [moveplayerviewcontroller.view recursivedescription]    UIView *videoview = [ Self.view viewwithtag:1002];    if ([Videoview iskindofclass:nsclassfromstring (@ "Mpvideoview")]) {        Videoview.transform = Cgaffinetransformmakerotation (M_PI * degrees/180.0);        Videoview.frame = Self.view.bounds;    }} @end
Four, HTML5 control video DirectionAdd in the Video tabstyle= "-webkit-transform:rotate (90deg);", but the control is also rotated. This will require the default playback controls to be hidden and the control to be painted, this slightly. v. Writing rotation metadata using ffmpegFor mp4 files that do not have rotation metadata, they can be written by tools such as FFmpeg. For example, the video needs to rotate clockwise 90 degrees display:ffmpeg-i input.mp4-c copy-metadata:s:v:0 rotate= output.mp4
Note: It is also possible to write values that are not 0, 90, 180, or 270, such as 45, if you wish.  
Vi. getting the video direction (angle)
+ (Nsuinteger) Degressfromvideofilewithurl: (Nsurl *) url{Nsuinteger degress = 0;    Avasset *asset = [Avasset Assetwithurl:url];    Nsarray *tracks = [Asset Trackswithmediatype:avmediatypevideo];        if ([tracks count] > 0) {avassettrack *videotrack = [tracks objectatindex:0];               Cgaffinetransform t = videotrack.preferredtransform;  if (t.a = = 0 && t.b = = 1.0 && t.c = -1.0 && t.d = 0) {//Portrait degress =        90;            }else if (t.a = = 0 && t.b = = -1.0 && t.c = 1.0 && T.D = 0) {//Portraitupsidedown        degress = 270;            }else if (t.a = = 1.0 && t.b = = 0 && t.c = 0 && T.D = 1.0) {//Landscaperight        degress = 0;            }else if (t.a = = -1.0 && t.b = = 0 && t.c = 0 && t.d = -1.0) {//Landscapeleft        degress = 180; }} return degress;}
Seven, according to the correct direction of the videoThe key point is to set the Appliespreferredtracktransform property of the Avassetimagegrnerator object to Yes.
+ (UIImage *) Extractimagefromvideofilewithurl: (Nsurl *) url{    nsdictionary *opts = @{ avurlassetpreferprecisedurationandtimingkey:@ (NO)};    Avurlasset *asset = [[Avurlasset alloc] Initwithurl:url options:opts];    Avassetimagegenerator *gen = [[Avassetimagegenerator alloc] initwithasset:asset];    Application direction    gen.appliespreferredtracktransform = YES;    Cmtime time = cmtimemakewithseconds (1);    Nserror *error = nil;    Cmtime Actualtime;    Cgimageref image = [Gen Copycgimageattime:time actualtime:&actualtime error:&error];    if (Error)    {        DLog (@ "%@%@", __function_file_line__,error);        return nil;    }    UIImage *thumb = [[UIImage alloc] initwithcgimage:image];    Cgimagerelease (image);       return thumb;}
Eight, the direction of real-time video processingWhen making a custom camera using Avfoundation, the captured video frame is stored in the CMSAMPLEBUFFERREF structure, and the color space can be set to sRGB or YUV. Perform some memory operations to achieve rotation. The following code is for YUV.
Note: This involves a large amount of memory copy operation, the actual application to weigh its pros and cons. The following code has not been tested.

1, RGB24 rotation 90 degrees

RGB24 rotation 90 degrees void Rgb24rotate90 (int8_t *des, const int8_t *src, int width, int height) {    if (!des | |!src) return;       int n = 0;    int linesize = width * 3;    int I, J;    Counterclockwise rotation for    (j = width; j > 0; j--) {for        (i = 0; i < height; i++) {            memccpy (&des[n], &src[line Size * i + j * 3-3], 0, 3);            n + = 3;        }    }    /*//clockwise rotation for    (j = 0; J < width; j + +) {for        (i = height; i > 0; i--) {            memccpy (&des[n], & Amp;src[linesize * (i-1) + J * 3-3], 0, 3);            n + = 3;        }    }    */} 

2, RGB24 rotation 90 degrees

YUV420 rotation 90 degrees void Yuv420rotate90 (int8_t *des, const int8_t *src, int width, int height) {    int i = 0, j = 0, n = 0;
   
    int HW = width/2, hh = HEIGHT/2;       Const int8_t *ptmp = src;    for (j = width; j > 0, j--) {for        (i = 0; i < height; i++) {            des[n++] = ptmp[width * i + j];        }    }       ptmp = src + width * height;    for (j = HW; J > 0, j--) {for        (i = 0; i < hh; i++) {            des[n++] = PTMP[HW * i + j];        }    }       ptmp = src + width * height * 5/4;    for (j = HW; J > 0, j--) {for        (i = 0; i < hh; i++) {            des[n++] = PTMP[HW * i + j];}}    }
   

Or:

Int8_t[] Rotateyuv420degree90 (int8_t[] data, int imagewidth, int imageheight) {    int8_t [] YUV = new int8_t[imagewidth* IMAGEHEIGHT*3/2];    Rotate the Y luma    int i = 0;    for (int x = 0;x < imagewidth;x++)    {for        (int y = imageheight-1;y >= 0;y--)        {            Yuv[i] = Data[y*imagewid TH+X];            i++;        }    }    Rotate the U and V color components    i = imagewidth*imageheight*3/2-1;    for (int x = imagewidth-1;x > 0;x=x-2)    {for        (int y = 0;y < imageheight/2;y++)        {            Yuv[i] = data[(im Agewidth*imageheight) + (y*imagewidth) +x];            i--;            Yuv[i] = data[(imagewidth*imageheight) + (y*imagewidth) + (x-1)];            i--;        }    }    return YUV;}
ix. References:Http://www.rosoo.net/a/201006/9689.html http://stackoverflow.com/questions/14167976/rotate-an-yuv-byte-array-on-android

[IOS] Some questions about video direction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.