Convert gif to mp4 in share extension

This commit is contained in:
Ilya Laktyushin 2019-10-07 08:17:19 +03:00
parent 993d668588
commit a9c719b4bc

View File

@ -155,7 +155,36 @@ private func preparedShareItem(account: Account, to peerId: PeerId, value: [Stri
} }
} }
if isGif { if isGif {
return standaloneUploadedFile(account: account, peerId: peerId, text: "", source: .data(data), mimeType: "animation/gif", attributes: [.ImageSize(size: image.size), .Animated, .FileName(fileName: fileName ?? "animation.gif")], hintFileIsLarge: data.count > 5 * 1024 * 1024) let convertedData = Signal<(Data, CGSize, Double, Bool), NoError> { subscriber in
let disposable = MetaDisposable()
let signalDisposable = TGGifConverter.convertGif(toMp4: data).start(next: { next in
if let result = next as? NSDictionary, let path = result["path"] as? String, let convertedData = try? Data(contentsOf: URL(fileURLWithPath: path)), let duration = result["duration"] as? Double {
subscriber.putNext((convertedData, image.size, duration, true))
subscriber.putCompletion()
}
}, error: { _ in
subscriber.putNext((data, image.size, 0, false))
subscriber.putCompletion()
}, completed: nil)
disposable.set(ActionDisposable {
signalDisposable?.dispose()
})
return disposable
}
return convertedData
|> castError(Void.self)
|> mapToSignal { data, dimensions, duration, converted in
var attributes: [TelegramMediaFileAttribute] = []
let mimeType: String
if converted {
mimeType = "video/mp4"
attributes = [.Video(duration: Int(duration), size: dimensions, flags: [.supportsStreaming]), .Animated, .FileName(fileName: "animation.mp4")]
} else {
mimeType = "animation/gif"
attributes = [.ImageSize(size: dimensions), .Animated, .FileName(fileName: fileName ?? "animation.gif")]
}
return standaloneUploadedFile(account: account, peerId: peerId, text: "", source: .data(data), mimeType: mimeType, attributes: attributes, hintFileIsLarge: data.count > 5 * 1024 * 1024)
|> mapError { _ -> Void in return Void() } |> mapError { _ -> Void in return Void() }
|> mapToSignal { event -> Signal<PreparedShareItem, Void> in |> mapToSignal { event -> Signal<PreparedShareItem, Void> in
switch event { switch event {
@ -165,6 +194,7 @@ private func preparedShareItem(account: Account, to peerId: PeerId, value: [Stri
return .single(.done(.media(media))) return .single(.done(.media(media)))
} }
} }
}
} else { } else {
let scaledImage = TGScaleImageToPixelSize(image, CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale).fitted(CGSize(width: 1280.0, height: 1280.0)))! let scaledImage = TGScaleImageToPixelSize(image, CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale).fitted(CGSize(width: 1280.0, height: 1280.0)))!
let imageData = scaledImage.jpegData(compressionQuality: 0.54)! let imageData = scaledImage.jpegData(compressionQuality: 0.54)!