Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resulting video is small and in the middle of the screen #30

Open
lsamaria opened this issue Jul 8, 2020 · 3 comments
Open

Resulting video is small and in the middle of the screen #30

lsamaria opened this issue Jul 8, 2020 · 3 comments

Comments

@lsamaria
Copy link

lsamaria commented Jul 8, 2020

I have several videos that I stitch together using an AVMutableComposition(). After it's done the only way that I found to pass it to the NextLevelSessionExporter was to add it to a player and then extract the player's asset so that I can get the naturalSize. After the NextLevelSessionExporter is finished and I play the video, the video is centered in the middle of the screen.

This screenshot is the problem, the blue circle is a button and has nothing to do with it:

IMG_4631

If I use a regular AVExportSession() the video takes up the screen like it's supposed to so there is something going wrong with NextLevelSessionExporter.

   let mixComposition = AVMutableComposition()
   // ...

   let item = AVPlayerItem(asset: mixComposition)
   let player = AVPlayer()
   player.replaceCurrentItem(with: item)
   let asset = player!.currentItem.asset
   guard let videoTrack = asset.tracks(withMediaType: AVMediaType.video).first else { return }

    let exporter = NextLevelSessionExporter(withAsset: asset)
    exporter.outputFileType = AVFileType.mp4
    let tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(ProcessInfo().globallyUniqueString).appendingPathExtension("mp4")
    exporter.outputURL = tmpURL
    
    let compressionDict: [String: Any] = [
        AVVideoAverageBitRateKey: NSNumber(integerLiteral: 250000),
        AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel as String
    ]
    
    exporter.videoOutputConfiguration = [
        AVVideoCodecKey : AVVideoCodecType.h264,
        AVVideoHeightKey: videoTrack.naturalSize.height,
        AVVideoWidthKey : videoTrack.naturalSize.width,
        AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
        AVVideoCompressionPropertiesKey: compressionDict
    ]
    exporter.audioOutputConfiguration = [
        AVFormatIDKey: kAudioFormatMPEG4AAC,
        AVEncoderBitRateKey: NSNumber(integerLiteral: 128000),
        AVNumberOfChannelsKey: NSNumber(integerLiteral: 2),
        AVSampleRateKey: NSNumber(value: Float(44100))
    ]

    exporter.export(progressHandler: { (progress) in
        print(progress)
    }, completionHandler: { result in
        switch result {
        case .success(let status):
            switch status {
            case .completed:
                print("NextLevelSessionExporter, export completed, \(exporter.outputURL?.description ?? "")")
                
                guard let videoURL = exporter.outputURL else { return }
                
            default:
                print("NextLevelSessionExporter, did not complete")
            }
        case .failure(let error):
            print("NextLevelSessionExporter, failed to export \(error)")
        }
    })
@lsamaria
Copy link
Author

lsamaria commented Jul 9, 2020

I had to change the exporter.videoOutputConfiguration settings and for some weird reason I had to invert the width and height. The HeightKey uses the width and WidthKey uses the height.

Outside of these issues this pod is EXCELLENT!!!

let mixComposition = AVMutableComposition()
// ...

let item = AVPlayerItem(asset: mixComposition)
let player = AVPlayer()
player.replaceCurrentItem(with: item)
let asset = player!.currentItem.asset
guard let videoTrack = asset.tracks(withMediaType: AVMediaType.video).first else { return }

exporter.videoOutputConfiguration = [
        AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 1100000],
        AVVideoCodecKey: AVVideoCodecType.h264,
        AVVideoHeightKey: videoTrack.naturalSize.width, // height uses width
        AVVideoWidthKey: videoTrack.naturalSize.height // width uses height
 ]

@piemonte
Copy link
Contributor

thanks @lsamaria !

i appreciate the project interest. seems like a video orientation issue. will check into sometime

@piemonte piemonte reopened this Jul 23, 2020
@puelocesar
Copy link

puelocesar commented Apr 13, 2021

I solved a similar issue I had by calculating the width and height based on this:
videoTrack.naturalSize.applying(videoTrack.preferredTransform)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants