以纵向模式拍摄视频但使用媒体播放器播放时方向错误

时间:2016-10-13 09:37:12

标签: android html5 ffmpeg android-mediarecorder

我正在尝试制作自定义视频应用。该应用程序使用mediarecorder捕获视频,并将其上传到在html5媒体播放器上播放的网站。问题在于,当我以纵向模式捕获视频时,网站和vlc播放器上的播放视频将其方向偏离90度。我在互联网上尝试了各种解决方案,但没有一个适合我。

以下是我设置媒体记录器的代码:

 private bool prepareMediaRecorder() {

        Android.Hardware.Camera.Parameters p = mCamera.GetParameters();
        List<Android.Hardware.Camera.Size> previewSizes = p.SupportedPreviewSizes.ToList();

        //mCamera = GetCameraInstance();
        //setCameraDisplayOrientation(Activity, cameraId, mCamera);

        mediaRecorder = new MediaRecorder();
        mCamera.Unlock();
        mediaRecorder.SetCamera(mCamera);

        mediaRecorder.SetAudioSource(AudioSource.Camcorder);
        mediaRecorder.SetVideoSource(VideoSource.Camera);
        mediaRecorder.SetOrientationHint(CameraS.result);

        if (currentapiVersion >= 16)
        {
            mediaRecorder.SetProfile(CamcorderProfile.Get(CamcorderQuality.High)); // requires API Level 8 or higher
        }
        else
        {
            mediaRecorder.SetProfile(CamcorderProfile.Get(CamcorderQuality.Q480p)); // requires API Level 8 or higher
        }

        //mediaRecorder.SetVideoFrameRate(30);
        mediaRecorder.SetOutputFile(localPath);
        mediaRecorder.SetMaxDuration(600000); // Set max duration 60 sec.
        mediaRecorder.SetMaxFileSize(50000000); // Set max file size 50M
        mediaRecorder.SetVideoSize(previewSizes[0].Width, previewSizes[0].Height);
        mediaRecorder.SetPreviewDisplay(mPreview.Holder.Surface);

        mediaRecorder.SetOrientationHint(getDisplayOrientationAngle());

        try
        {
            mediaRecorder.Prepare();
        }
        catch (IllegalStateException e)
        {
            releaseMediaRecorder();
            return false;
        }
        catch (Java.IO.IOException e)
        {
            releaseMediaRecorder();
            return false;
        }
        return true;

}

public int getDisplayOrientationAngle() {
        //Log.e("", "setDisplayOrientationAngle is call");
        int angle;


        var mDisplayRotation = Activity.WindowManager.DefaultDisplay.Rotation;
        // switch (MeasurementNativeActivity.DisplayRotation) {
        switch (mDisplayRotation)
        {
            case SurfaceOrientation.Rotation0: // This is display orientation
                angle = 90; // This is camera orientation
                break;
            case SurfaceOrientation.Rotation90:
                angle = 0;
                break;
            case SurfaceOrientation.Rotation180:
                angle = 270;
                break;
            case SurfaceOrientation.Rotation270:
                angle = 180;
                break;
            default:
                angle = 90;
                break;
        }
        //Log.v("", "media recorder displayRotation: " + mDisplayRotation);
        //Log.v("", "media recorder angle: " + angle);
        return angle;

}

在手机应用上播放时,视频的方向很好,但在网站上却没有。我尝试使用FFMPEG编码器顺时针旋转视频。这似乎解决了视频的方向,但视频的宽高比都搞砸了。视频垂直拉伸并离开屏幕。 我用来旋转和编码视频的命令如下:

-y -i inputPath -vf scale=640:480,setsar=1,transpose=1 -strict experimental -r 25 -vcodec mpeg4 -b 1000k ab 48000 -ac 2 -ar 22050 destinationPath

如果有人可以帮我修改视频的方向或编码视频后的宽高比,我将非常感激。

干杯!

编辑。

这是输入文件的读数:

General
Complete name                  : C:\Users\Ahmed\Desktop\9adcdb56-500e-4d3f-8fb2-ccf20c680422_yt.mp4
Format                         : MPEG-4
Format profile                 : Base Media
Codec ID                       : isom
File size                      : 6.30 MiB
Duration                       : 12s 437ms
Overall bit rate               : 4 251 Kbps
Encoded date                   : UTC 2016-05-29 23:54:30
Tagged date                    : UTC 2016-05-29 23:54:30

Video
ID                             : 1
Format                         : AVC
Format/Info                    : Advanced Video Codec
Format profile                 : Baseline@L3.1
Format settings, CABAC         : No
Format settings, ReFrames      : 1 frame
Format settings, GOP           : M=1, N=31
Codec ID                       : avc1
Codec ID/Info                  : Advanced Video Coding
Duration                       : 12s 323ms
Bit rate                       : 3 967 Kbps
Width                          : 1 280 pixels
Height                         : 720 pixels
Display aspect ratio           : 16:9
Rotation                       : 90°
Frame rate mode                : Variable
Frame rate                     : 29.700 fps
Minimum frame rate             : 29.354 fps
Maximum frame rate             : 30.040 fps
Color space                    : YUV
Chroma subsampling             : 4:2:0
Bit depth                      : 8 bits
Scan type                      : Progressive
Bits/(Pixel*Frame)             : 0.145
Stream size                    : 5.83 MiB (92%)
Title                          : VideoHandle
Language                       : English
Encoded date                   : UTC 2016-05-29 23:54:30
Tagged date                    : UTC 2016-05-29 23:54:30

Audio
ID                             : 2
Format                         : AAC
Format/Info                    : Advanced Audio Codec
Format profile                 : LC
Codec ID                       : 40
Duration                       : 12s 437ms
Source duration                : 12s 454ms
Bit rate mode                  : Constant
Bit rate                       : 128 Kbps
Nominal bit rate               : 96.0 Kbps
Channel(s)                     : 2 channels
Channel positions              : Front: L R
Sampling rate                  : 48.0 KHz
Compression mode               : Lossy
Stream size                    : 194 KiB (3%)
Source stream size             : 194 KiB (3%)
Title                          : SoundHandle
Language                       : English
Encoded date                   : UTC 2016-05-29 23:54:30
Tagged date                    : UTC 2016-05-29 23:54:30
mdhd_Duration                  : 12454

1 个答案:

答案 0 :(得分:1)

开头应为-y -noautorotate -i inputPath -vf transpose=1,scale=360:640,setsar=1 -metadata:s:v rotate=0

如果旋转标签未被剥离,某些播放器仍会显示旋转的视频。此外,一岁以下的ffmpeg版本将自动剥离标签并旋转视频。如果您手动应用旋转,这可能会导致意外行为。