Created
March 25, 2020 14:13
-
-
Save christianselig/42dfffeaaf2ceb45a6e8cde6a0339c86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
let data = try! Data(contentsOf: URL(string: "https://i.imgur.com/dXxP7a9.mp4")!) | |
let fileName = String(format: "%@_%@", ProcessInfo.processInfo.globallyUniqueString, "html5gif.mov") | |
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName) | |
try! data.write(to: fileURL, options: [.atomic]) | |
tweakMetaData(forVideoAtURL: fileURL) | |
} | |
func tweakMetaData(forVideoAtURL url: URL) { | |
let asset = AVURLAsset(url: url) | |
let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough)! | |
let exportFileName = String(format: "%@_%@", ProcessInfo.processInfo.globallyUniqueString, "html5gif-redone.mov") | |
let exportFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(exportFileName) | |
exportSession.outputURL = exportFileURL | |
exportSession.outputFileType = .mov | |
var metaData = asset.metadata | |
let mutableMetaDataItem = AVMutableMetadataItem() | |
mutableMetaDataItem.keySpace = .quickTimeUserData | |
mutableMetaDataItem.key = NSString("LOOP") | |
mutableMetaDataItem.value = NSNumber(integerLiteral: 0) | |
metaData.append(mutableMetaDataItem) | |
exportSession.metadata = metaData | |
exportSession.exportAsynchronously { | |
if exportSession.status == .completed { | |
print("YAY") | |
UISaveVideoAtPathToSavedPhotosAlbum(exportFileURL.path, nil, nil, nil) | |
} else { | |
print("oh no") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment