locked
How to record a video in portrait orientation? RRS feed

  • Question

  • I am developing an app which uses MediaCapture class to record videos. I want to give user freedom to record video in either portrait or landsapce by just rotating the device. Please note my app has fixed portrait orientation. Due to fixed orientation, I am using OrientationChanged event of class SimpleOrientationSensor.

    Now I have noticed that the portait video recorded in default camera app and in my app has some differences. You can see the difference in below given images. All the videos are in 720p resolution. The player images are of portrait locked orientation. The code is also given below. Can any one explain me what's wrong with my code to record the portrait video? Thanks!

    Video thumbnails in explorerVideo in Photos app in Windows Phone 8.1 device

    MediaCaptureInitializationSettings set;
    MediaCapture _camCapture;
    VideoRotation videoRotation = VideoRotation.None;
    bool _isRecording = false;
    
    // Camera resource disposal is handled properly
    private async Task InitializeCamera()
    {
    	try
    	{
    		if (set == null)
    		{
    			set = new MediaCaptureInitializationSettings();
    
    			DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
    										.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
    
    			if (deviceID != null)
    			{
    				set.VideoDeviceId = deviceID.Id;
    			}
    		}
    
    		_camCapture = new MediaCapture();
    		await _camCapture.InitializeAsync(set);
    		VideoView.Source = _camCapture;    //VideoView is CaptureElement object in XAML
    		_camCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
    		await _camCapture.StartPreviewAsync();
    	}
    	catch (Exception ex)
    	{
    	}
    }
    
    private async void OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs e)
    {
    	try
    	{
    		await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    		{
    			DetectCurrentOrintation(e.Orientation);
    		}); 
    	}
    	catch (Exception ex)
    	{
    	}
    }
    
    private void DetectCurrentOrintation(SimpleOrientation orientation)
    {
    	try
    	{
    		switch (orientation)
    		{
    			case SimpleOrientation.NotRotated:
    				videoRotation = VideoRotation.Clockwise90Degrees;
    				break;
    			case SimpleOrientation.Rotated90DegreesCounterclockwise:
    				videoRotation = VideoRotation.None;
    				break;
    			case SimpleOrientation.Rotated180DegreesCounterclockwise:
    				videoRotation = VideoRotation.Clockwise270Degrees;
    				break;
    			case SimpleOrientation.Rotated270DegreesCounterclockwise:
    				videoRotation = VideoRotation.Clockwise180Degrees;
    				break;
    			default:
    				break;
    		} 
    	}
    	catch (Exception ex)
    	{
    	}
    }
    
    private async void btnRecordVideo_Click(object sender, RoutedEventArgs e)
    {
    	try
    	{
    		if (!_isRecording)
    		{
    			var _videoRecording = await MyFolderInLocalFolder.CreateFileAsync("testRecord.mp4", CreationCollisionOption.GenerateUniqueName);
    			MediaEncodingProfile mEncode = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
    			
    			_isRecording = true;
    
    			//Two videos are with SetRecordRotation(value) and 
    			//for two other I commeted out the line.
    			_camCapture.SetRecordRotation(videoRotation);
    			await _camCapture.StartRecordToStorageFileAsync(mEncode, _videoRecording);
    		}
    		else
    		{
    			await _camCapture.StopRecordAsync();
    			_isRecording = false;
    		}
    	}
    	catch (Exception ex)
    	{
    	}
    }


    Tuesday, December 30, 2014 12:29 PM

All replies

  • Hello,

    I'm not sure what you are asking here. Setting the record rotation should add the necessary metadata in the recorded file to enable it to playback in the proper aspect ratio. I think your image might have more data but unfortunately it is just too small to see.

    Please let me know what I am missing.

    Thanks,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, December 30, 2014 9:27 PM
  • My question is why the portrait recorded video are different from the default camera app with compare to my app (even if I use rotation while recording). Please open the image in new tab, you will see the image in full screen.

    http://i.imgur.com/jHq7icH.png

    http://i.imgur.com/9wC3w6N.png

    Due to such issue, I am not able to resize the video with help of MediaComposition and EncodingProfile.

    I tried to resize the portrait video with help of the API. The portrait video recorded via default camera resized accurately while the video recorded in my app doesn't resize properly, it adds black box (frame) surrounding it and hence the video content become so smaller in a rectangle form.

    Wednesday, December 31, 2014 5:35 AM
  • Hello James,

    If you need any other more details, I would happy to provide you. Please look into the issue, only that issue is being obstruction for my app launch.

    Tuesday, January 20, 2015 8:37 AM
  • Hello,

    I would recommend that you modify your code to match the Media Capture sample's advanced capture scenario. Instead of using SimpleOrientationSensorOrientationChangedEventArgs.Orientation use Windows.Graphics.Display.DisplayInformation.

    Here is the code from the sample:

    private void DisplayInfo_OrientationChanged(Windows.Graphics.Display.DisplayInformation sender, object args)
    {
        m_displayOrientation = sender.CurrentOrientation;
        OrientationChanged();
    }
    
    private void PrepareForVideoRecording()
    {
        try
        {
            if (m_mediaCaptureMgr == null)
            {
                return;
            }
    
            bool counterclockwiseRotation = m_bReversePreviewRotation;
    
            if (m_bRotateVideoOnOrientationChange)
            {
                m_mediaCaptureMgr.SetRecordRotation(VideoRotationLookup(m_displayOrientation, counterclockwiseRotation));
            }
            else
            {
                m_mediaCaptureMgr.SetRecordRotation(Windows.Media.Capture.VideoRotation.None);
            }
        }
        catch (Exception exception)
        {
            ShowExceptionMessage(exception);
        }
    }
    
    private Windows.Media.Capture.VideoRotation VideoRotationLookup(
        Windows.Graphics.Display.DisplayOrientations displayOrientation,
        bool counterclockwise)
    {
        switch (displayOrientation)
        {
            case Windows.Graphics.Display.DisplayOrientations.Landscape:
                return Windows.Media.Capture.VideoRotation.None;
    
            case Windows.Graphics.Display.DisplayOrientations.Portrait:
                return (counterclockwise) ? Windows.Media.Capture.VideoRotation.Clockwise270Degrees :
                    Windows.Media.Capture.VideoRotation.Clockwise90Degrees;
    
            case Windows.Graphics.Display.DisplayOrientations.LandscapeFlipped:
                return Windows.Media.Capture.VideoRotation.Clockwise180Degrees;
    
            case Windows.Graphics.Display.DisplayOrientations.PortraitFlipped:
                return (counterclockwise) ? Windows.Media.Capture.VideoRotation.Clockwise90Degrees :
                    Windows.Media.Capture.VideoRotation.Clockwise270Degrees;
    
            default:
                return Windows.Media.Capture.VideoRotation.None;
        }
    }

    I hope this helps,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Thursday, January 29, 2015 9:53 PM
  • As I said earlier, my app has fixed portrait orientation, I can't use OrientationChanged event. It's not being called.
    Friday, January 30, 2015 9:15 AM
  • Hello,

    Very sorry I missed that initially. I'm not sure why things aren't working. I haven't been able to verify your VideoRotationLookup function however.

    Probably the best thing to do is to have you upload a very small Visual Studio project that only includes code the reproduces the issue. Please zip this code up (no RAR), upload it to your OneDrive and post a link here. I'll grab it when I have a few minutes and take a look at it for you.

    Thanks,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Monday, February 2, 2015 8:40 PM