如何以纵向录制视频?

时间:2014-12-30 10:01:47

标签: c# video camera windows-phone-8.1 media

我正在为Windows Phone 8.1开发一个应用程序,它使用MediaCapture类来录制视频。我想通过旋转设备让用户自由地以肖像或景观录制视频。请注意我的应用具有固定的纵向方向。由于方向固定,我使用的是SimpleOrientationSensor类的OrientationChanged事件。

现在我注意到默认相机应用程序和我的应用程序中录制的肖像视频有一些差异。您可以在下面给出的图像中看到差异。所有视频均为720p分辨率。玩家图像具有纵向锁定方向。代码也在下面给出。任何人都可以向我解释我的代码记录肖像视频的错误吗?谢谢!

enter image description here enter image description here

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)
    {
    }
}

1 个答案:

答案 0 :(得分:0)

Microsoft github页面上有一个相关的示例,尽管它们的目标是Windows 10.但是,API应该适用于8 / 8.1。

UniversalCameraSample:此视频捕获视频并支持纵向和横向方向。以下是相关部分:

// Create storage file in Pictures Library
var videoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("SimpleVideo.mp4", CreationCollisionOption.GenerateUniqueName);

var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);

// Calculate rotation angle, taking mirroring into account if necessary
var rotationAngle = 360 - ConvertDeviceOrientationToDegrees(GetCameraOrientation());
encodingProfile.Video.Properties.Add(RotationKey, PropertyValue.CreateInt32(rotationAngle));

await _mediaCapture.StartRecordToStorageFileAsync(encodingProfile, videoFile);

仔细查看示例,了解如何首先获取相机的方向(在我发布的代码段中调用它)。您通常不希望调用SetRecordRotation(或SetPreviewRotation),因为这些只会对添加旋转元数据产生影响。

观看最近//版本/会议中的camera session,其中包括对一些相机示例的一些演练,并为您提供有关示例的更多背景信息。