NextLevel / NextLevelSessionExporter

đŸ”„ Export and transcode media in Swift
http://nextlevel.engineering
MIT License
261 stars 47 forks source link

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

Open lsamaria opened 4 years ago

lsamaria commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

thanks @lsamaria !

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

puelocesar commented 3 years ago

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