mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-07 16:11:13 +00:00
Fix video export dimensions & orientation, revert to h264 for now
This commit is contained in:
parent
53ddac7c14
commit
2749d3a2fe
@ -919,13 +919,14 @@ extension CodableToolValue: Codable {
|
|||||||
public func recommendedVideoExportConfiguration(values: MediaEditorValues) -> MediaEditorVideoExport.Configuration {
|
public func recommendedVideoExportConfiguration(values: MediaEditorValues) -> MediaEditorVideoExport.Configuration {
|
||||||
let compressionProperties: [String: Any] = [
|
let compressionProperties: [String: Any] = [
|
||||||
AVVideoAverageBitRateKey: 2000000,
|
AVVideoAverageBitRateKey: 2000000,
|
||||||
AVVideoProfileLevelKey: kVTProfileLevel_HEVC_Main_AutoLevel
|
//AVVideoProfileLevelKey: kVTProfileLevel_HEVC_Main_AutoLevel
|
||||||
//AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
|
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
|
||||||
//AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCABAC
|
AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCABAC
|
||||||
]
|
]
|
||||||
|
|
||||||
let videoSettings: [String: Any] = [
|
let videoSettings: [String: Any] = [
|
||||||
AVVideoCodecKey: AVVideoCodecType.hevc,
|
AVVideoCodecKey: AVVideoCodecType.h264,
|
||||||
|
//AVVideoCodecKey: AVVideoCodecType.hevc,
|
||||||
AVVideoCompressionPropertiesKey: compressionProperties,
|
AVVideoCompressionPropertiesKey: compressionProperties,
|
||||||
AVVideoWidthKey: 720,
|
AVVideoWidthKey: 720,
|
||||||
AVVideoHeightKey: 1280
|
AVVideoHeightKey: 1280
|
||||||
|
@ -14,7 +14,7 @@ enum ExportWriterStatus {
|
|||||||
|
|
||||||
protocol MediaEditorVideoExportWriter {
|
protocol MediaEditorVideoExportWriter {
|
||||||
func setup(configuration: MediaEditorVideoExport.Configuration, outputPath: String)
|
func setup(configuration: MediaEditorVideoExport.Configuration, outputPath: String)
|
||||||
func setupVideoInput(configuration: MediaEditorVideoExport.Configuration, inputTransform: CGAffineTransform?)
|
func setupVideoInput(configuration: MediaEditorVideoExport.Configuration)
|
||||||
func setupAudioInput(configuration: MediaEditorVideoExport.Configuration)
|
func setupAudioInput(configuration: MediaEditorVideoExport.Configuration)
|
||||||
|
|
||||||
func startWriting() -> Bool
|
func startWriting() -> Bool
|
||||||
@ -55,28 +55,17 @@ public final class MediaEditorVideoAVAssetWriter: MediaEditorVideoExportWriter {
|
|||||||
writer.shouldOptimizeForNetworkUse = configuration.shouldOptimizeForNetworkUse
|
writer.shouldOptimizeForNetworkUse = configuration.shouldOptimizeForNetworkUse
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupVideoInput(configuration: MediaEditorVideoExport.Configuration, inputTransform: CGAffineTransform?) {
|
func setupVideoInput(configuration: MediaEditorVideoExport.Configuration) {
|
||||||
guard let writer = self.writer else {
|
guard let writer = self.writer else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let videoInput: AVAssetWriterInput
|
let videoInput = AVAssetWriterInput(mediaType: .video, outputSettings: configuration.videoSettings)
|
||||||
if let transform = inputTransform {
|
|
||||||
let size = CGSize(width: configuration.videoSettings[AVVideoWidthKey] as! Int, height: configuration.videoSettings[AVVideoHeightKey] as! Int)
|
|
||||||
let transformedSize = size.applying(transform.inverted())
|
|
||||||
var videoSettings = configuration.videoSettings
|
|
||||||
videoSettings[AVVideoWidthKey] = abs(transformedSize.width)
|
|
||||||
videoSettings[AVVideoHeightKey] = abs(transformedSize.height)
|
|
||||||
videoInput = AVAssetWriterInput(mediaType: .video, outputSettings: videoSettings)
|
|
||||||
videoInput.transform = transform
|
|
||||||
} else {
|
|
||||||
videoInput = AVAssetWriterInput(mediaType: .video, outputSettings: configuration.videoSettings)
|
|
||||||
}
|
|
||||||
videoInput.expectsMediaDataInRealTime = false
|
videoInput.expectsMediaDataInRealTime = false
|
||||||
|
|
||||||
let sourcePixelBufferAttributes = [
|
let sourcePixelBufferAttributes = [
|
||||||
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
|
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
|
||||||
kCVPixelBufferWidthKey as String: 1080,
|
kCVPixelBufferWidthKey as String: UInt32(configuration.dimensions.width),
|
||||||
kCVPixelBufferHeightKey as String: 1920
|
kCVPixelBufferHeightKey as String: UInt32(configuration.dimensions.height)
|
||||||
]
|
]
|
||||||
self.adaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoInput, sourcePixelBufferAttributes: sourcePixelBufferAttributes)
|
self.adaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoInput, sourcePixelBufferAttributes: sourcePixelBufferAttributes)
|
||||||
|
|
||||||
@ -285,10 +274,6 @@ public final class MediaEditorVideoExport {
|
|||||||
self.duration.set(CMTime(seconds: 3, preferredTimescale: 1))
|
self.duration.set(CMTime(seconds: 3, preferredTimescale: 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.configuration.values.requiresComposing {
|
|
||||||
self.composer = MediaEditorComposer(account: self.account, values: self.configuration.values, dimensions: self.configuration.dimensions)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch self.subject {
|
switch self.subject {
|
||||||
case let .video(asset):
|
case let .video(asset):
|
||||||
self.setupWithAsset(asset)
|
self.setupWithAsset(asset)
|
||||||
@ -297,6 +282,13 @@ public final class MediaEditorVideoExport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func setupComposer() {
|
||||||
|
guard self.composer == nil else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.composer = MediaEditorComposer(account: self.account, values: self.configuration.values, dimensions: self.configuration.dimensions)
|
||||||
|
}
|
||||||
|
|
||||||
private func setupWithAsset(_ asset: AVAsset) {
|
private func setupWithAsset(_ asset: AVAsset) {
|
||||||
self.reader = try? AVAssetReader(asset: asset)
|
self.reader = try? AVAssetReader(asset: asset)
|
||||||
guard let reader = self.reader else {
|
guard let reader = self.reader else {
|
||||||
@ -315,16 +307,14 @@ public final class MediaEditorVideoExport {
|
|||||||
|
|
||||||
let videoTracks = asset.tracks(withMediaType: .video)
|
let videoTracks = asset.tracks(withMediaType: .video)
|
||||||
if (videoTracks.count > 0) {
|
if (videoTracks.count > 0) {
|
||||||
let videoOutput: AVAssetReaderOutput
|
let outputSettings: [String : Any]
|
||||||
let inputTransform: CGAffineTransform?
|
if let videoTrack = videoTracks.first, videoTrack.preferredTransform.isIdentity && !self.configuration.values.requiresComposing {
|
||||||
if self.composer == nil {
|
outputSettings = [kCVPixelBufferPixelFormatTypeKey as String: [kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]]
|
||||||
videoOutput = AVAssetReaderTrackOutput(track: videoTracks.first!, outputSettings: [kCVPixelBufferPixelFormatTypeKey as String: [kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]])
|
|
||||||
inputTransform = videoTracks.first!.preferredTransform
|
|
||||||
} else {
|
} else {
|
||||||
videoOutput = AVAssetReaderTrackOutput(track: videoTracks.first!, outputSettings: [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA])
|
outputSettings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA]
|
||||||
inputTransform = nil
|
self.setupComposer()
|
||||||
}
|
}
|
||||||
|
let videoOutput = AVAssetReaderTrackOutput(track: videoTracks.first!, outputSettings: outputSettings)
|
||||||
videoOutput.alwaysCopiesSampleData = false
|
videoOutput.alwaysCopiesSampleData = false
|
||||||
if reader.canAdd(videoOutput) {
|
if reader.canAdd(videoOutput) {
|
||||||
reader.add(videoOutput)
|
reader.add(videoOutput)
|
||||||
@ -334,7 +324,7 @@ public final class MediaEditorVideoExport {
|
|||||||
}
|
}
|
||||||
self.videoOutput = videoOutput
|
self.videoOutput = videoOutput
|
||||||
|
|
||||||
writer.setupVideoInput(configuration: self.configuration, inputTransform: inputTransform)
|
writer.setupVideoInput(configuration: self.configuration)
|
||||||
} else {
|
} else {
|
||||||
self.videoOutput = nil
|
self.videoOutput = nil
|
||||||
}
|
}
|
||||||
@ -363,12 +353,14 @@ public final class MediaEditorVideoExport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func setupWithImage(_ image: UIImage) {
|
private func setupWithImage(_ image: UIImage) {
|
||||||
|
self.setupComposer()
|
||||||
|
|
||||||
self.writer = MediaEditorVideoAVAssetWriter()
|
self.writer = MediaEditorVideoAVAssetWriter()
|
||||||
guard let writer = self.writer else {
|
guard let writer = self.writer else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writer.setup(configuration: self.configuration, outputPath: self.outputPath)
|
writer.setup(configuration: self.configuration, outputPath: self.outputPath)
|
||||||
writer.setupVideoInput(configuration: self.configuration, inputTransform: nil)
|
writer.setupVideoInput(configuration: self.configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func finish() {
|
private func finish() {
|
||||||
|
@ -1528,7 +1528,7 @@ public final class MediaEditorScreen: ViewController {
|
|||||||
duration = 5.0
|
duration = 5.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.completion(.video(video: videoResult, coverImage: nil, values: mediaEditor.values, duration: duration, dimensions: PixelDimensions(width: 1080, height: 1920), caption: caption), { [weak self] in
|
self.completion(.video(video: videoResult, coverImage: nil, values: mediaEditor.values, duration: duration, dimensions: PixelDimensions(width: 720, height: 1280), caption: caption), { [weak self] in
|
||||||
self?.node.animateOut(finished: true, completion: { [weak self] in
|
self?.node.animateOut(finished: true, completion: { [weak self] in
|
||||||
self?.dismiss()
|
self?.dismiss()
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user