Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created March 25, 2020 14:13
Show Gist options
  • Save christianselig/42dfffeaaf2ceb45a6e8cde6a0339c86 to your computer and use it in GitHub Desktop.
Save christianselig/42dfffeaaf2ceb45a6e8cde6a0339c86 to your computer and use it in GitHub Desktop.
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