ios - Swift:视频以一种尺寸录制,但以错误的尺寸呈现

标签 ios swift video avplayer pbjvision

目标是使用 Swift 在设备上捕获全屏视频。在下面的代码中,视频捕获似乎发生在全屏(同时录制相机预览使用全屏),但视频的呈现发生在不同的分辨率。特别是对于 5S,捕获似乎发生在 320x568,但渲染发生在 320x480

如何捕获和渲染全屏视频?

视频采集代码:

private func initPBJVision() {
    // Store PBJVision in var for convenience
    let vision = PBJVision.sharedInstance()

    // Configure PBJVision
    vision.delegate = self
    vision.cameraMode = PBJCameraMode.Video
    vision.cameraOrientation = PBJCameraOrientation.Portrait
    vision.focusMode = PBJFocusMode.ContinuousAutoFocus
    vision.outputFormat = PBJOutputFormat.Preset
    vision.cameraDevice = PBJCameraDevice.Back

    // Let taps start/pause recording
    let tapHandler = UITapGestureRecognizer(target: self, action: "doTap:")
    view.addGestureRecognizer(tapHandler)

    // Log status
    print("Configured PBJVision")
}


private func startCameraPreview() {
    // Store PBJVision in var for convenience
    let vision = PBJVision.sharedInstance()

    // Connect PBJVision camera preview to <videoView>
    // -- Get preview width
    let deviceWidth = CGRectGetWidth(view.frame)
    let deviceHeight = CGRectGetHeight(view.frame)

    // -- Configure PBJVision's preview layer
    let previewLayer = vision.previewLayer
    previewLayer.frame = CGRectMake(0, 0, deviceWidth, deviceHeight)
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    ...
}

视频渲染代码:

func exportVideo(fileUrl: NSURL) {
    // Create main composition object
    let videoAsset = AVURLAsset(URL: fileUrl, options: nil)
    let mainComposition = AVMutableComposition()
    let compositionVideoTrack = mainComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
    let compositionAudioTrack = mainComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))

    // -- Extract and apply video & audio tracks to composition
    let sourceVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0]
    let sourceAudioTrack = videoAsset.tracksWithMediaType(AVMediaTypeAudio)[0]
    do {
        try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceVideoTrack, atTime: kCMTimeZero)
    } catch {
        print("Error with insertTimeRange. Video error: \(error).")
    }
    do {
        try compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceAudioTrack, atTime: kCMTimeZero)
    } catch {
        print("Error with insertTimeRange. Audio error: \(error).")
    }

    // Add text to video
    // -- Create video composition object
    let renderSize = compositionVideoTrack.naturalSize
    let videoComposition = AVMutableVideoComposition()
    videoComposition.renderSize = renderSize
    videoComposition.frameDuration = CMTimeMake(Int64(1), Int32(videoFrameRate))

    // -- Add instruction to  video composition object
    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
    let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack)
    instruction.layerInstructions = [videoLayerInstruction]
    videoComposition.instructions = [instruction]

    // -- Define video frame
    let videoFrame = CGRectMake(0, 0, renderSize.width, renderSize.height)
    print("Video Frame: \(videoFrame)")  // <-- Prints frame of 320x480 so render size already wrong here 
    ...

最佳答案

如果我没猜错,您似乎误解了设备屏幕宽度不等于相机预览(和捕获)大小这一事实。

previewLayervideoGravity 属性指示如何在层内拉伸(stretch)/调整预览。它不会影响捕获输出。

输出的实际帧大小取决于 sessionPreset当前 AVCaptureSession 的属性。正如我通过阅读 PBJVision lib 的 GitHub 存储库所了解的那样,它的单例具有此 setter (称为 captureSessionPreset)。您可以在 initPBJVision 方法中更改它。

There您可以找到 session 预设的可能值。

关于ios - Swift:视频以一种尺寸录制,但以错误的尺寸呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34704032/

相关文章:

video - FFMPEG hwaccel 将 WEBM 转换为 MP4 视频失败 : could not find codec parameters

ios - 带有两个来电的 CallKit

ios - 尝试使用 HealthKit 获取步数,但它总是返回 0.0

ios - 如何在点击推送通知时打开特定帖子

ios - 呈现 View Controller 时发生崩溃

linux - Ffmpeg concat images 并没有完成所有图像

ios - 如果连接到的 UIImageView 在 UIScrollView 下,是否可以使 Tap Gesture Recognizer 工作?

javascript - 卸载某些页面的js请求

objective-c - iap - 从 objective c 到 swift 的转换

java - 使用默认媒体播放器播放流视频