2016-01-10 8 views
13

目的は、Swiftを搭載したデバイスでフルスクリーンビデオをキャプチャすることです。下のコードでは、ビデオキャプチャはフルスクリーンで行われるように見えますが(カメラプレビューの録画ではフルスクリーンが使用されます)、ビデオのレンダリングは別の解像度で行われます。特に5Sの場合、キャプチャが320x568に起こるように見えますが、レンダリングは320x480に発生します。Swift:1つのサイズのビデオ記録が間違ったサイズでレンダリングする

フルスクリーンビデオをキャプチャしてレンダリングするにはどうすればよいですか?ビデオキャプチャ用

コード:

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 
    ... 
+0

あなたは 'naturalSize'値を見ましたか?それは何ですか?そして次に何をしていますか?あなたは 'AVAssetExportSession'を使っていますか? – rkyr

+0

@rkyrはい、 'renderSize'は' naturalSize'に等しいです。プリントアウトの時点でレンダリングサイズはすでに間違っています(320x480)ので、これ以上のコードは含まれていません。助言がありますか? – Crashalot

答えて

8

右、あなたが事実を誤解しているようだに等しいis'nデバイスの画面幅ことカメラのプレビュー(およびキャプチャ)サイズ。

previewLayervideoGravityプロパティは、レイヤー内でプレビューをストレッチ/フィットする方法を示します。キャプチャ出力には影響しません。

出力の実際のフレームサイズは、現在AVCaptureSessionのプロパティsessionPresetに依存します。そして私がPBJVisionのlibのGitHubリポジトリを読むことによって理解できるように、そのシングルトンはこれのためのセッターを持っています(captureSessionPresetと呼ばれます)。 initPBJVisionメソッド内で変更することができます。

Thereセッションプリセットの可能な値を見つけることができます。

+0

@rkyrに感謝しますが、ドキュメントのクラスから、 'sessionPreset'はビットレートに影響を与えますが、実際のディメンションには影響しないようですね。キャプチャの実際のサイズをどのように変更して、プレビューに合わせるのですか? – Crashalot

+0

@ Crashalot、それは次元を変えることによって画質を変えます。 APIでキャプチャフレームを設定できませんでした。 [Here](http://stackoverflow.com/a/14322378/5247504)誰かがそれをどのように扱うかを見つけることができます。 – rkyr

+0

こんにちは@rkyrあなたに連絡する最善の方法は何ですか?簡単な質問をしたいですか? – Crashalot

3

あなたはまた、友人(マイケルLowinは)私たちはいくつかの改善を支援するために、そのプロパティを突き止め、何か4Kをサポートするためのプロファイル、AVVideoProfileLevelKeyすなわちAVVideoProfileLevelH264HighAutoLevelを指定することができAVVideoWidthKeyAVVideoHeightKey を指定することができますSDAVAssetExportSession

をお試しください輸出品質。

+1

Lamarに感謝します。素敵なスタイルの編集 –

関連する問題