Skip to content

Instantly share code, notes, and snippets.

@christianselig
Last active March 26, 2020 02:17
Show Gist options
  • Save christianselig/2f06ca28cbace9a5889666c38aaa3d0e to your computer and use it in GitHub Desktop.
Save christianselig/2f06ca28cbace9a5889666c38aaa3d0e 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])
print("Download completed")
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.dataType = kCMMetadataBaseDataType_RawData as String
mutableMetaDataItem.keySpace = .quickTimeUserData
mutableMetaDataItem.key = NSString("loop")
var loopVal = Int32(0)
mutableMetaDataItem.value = NSData(bytes: &loopVal, length: MemoryLayout.size(ofValue: loopVal))
metaData.append(mutableMetaDataItem)
exportSession.metadata = metaData
exportSession.exportAsynchronously {
print("asset export finished")
if exportSession.status == .completed {
print("completed successfully")
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