Convert a Video to GIF in Swift

Cloudmersive
2 min readJun 28, 2021

--

Due to the constant influx of social media, push notifications, and other electronic distractions, capturing and holding a consumer’s attention has become a significant challenge. One way to do this is to deliver a short, entertaining message via GIF, which is short for Graphics Interchange Format. In this quick tutorial, we will demonstrate how you can use an API in Swift to convert any video to GIF.

To call the function, the only required parameters are the video file and your API key, but there are optional parameters for file URLs, max width/height, aspect ratio preservation, frame rate, start time, and time span as well.

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = [
[
"key": "inputFile",
"src": "/path/to/file",
"type": "file"
]] as [[String : Any]]
let boundary = "Boundary-\(UUID().uuidString)"
var body = ""
var error: Error? = nil
for param in parameters {
if param["disabled"] == nil {
let paramName = param["key"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if param["contentType"] != nil {
body += "\r\nContent-Type: \(param["contentType"] as! String)"
}
let paramType = param["type"] as! String
if paramType == "text" {
let paramValue = param["value"] as! String
body += "\r\n\r\n\(paramValue)\r\n"
} else {
let paramSrc = param["src"] as! String
let fileData = try NSData(contentsOfFile:paramSrc, options:[]) as Data
let fileContent = String(data: fileData, encoding: .utf8)!
body += "; filename=\"\(paramSrc)\"\r\n"
+ "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n"
}
}
}
body += "--\(boundary)--\r\n";
let postData = body.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/video/convert/to/gif")!,timeoutInterval: Double.infinity)
request.addValue("<string>", forHTTPHeaderField: "fileUrl")
request.addValue("<integer>", forHTTPHeaderField: "maxWidth")
request.addValue("<integer>", forHTTPHeaderField: "maxHeight")
request.addValue("<boolean>", forHTTPHeaderField: "preserveAspectRatio")
request.addValue("<integer>", forHTTPHeaderField: "frameRate")
request.addValue("<dateTime>", forHTTPHeaderField: "startTime")
request.addValue("<dateTime>", forHTTPHeaderField: "timeSpan")
request.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

With this, you can create GIFs out of your video content to engage your users. If you need to obtain an API key, you can do so by register for a free account on the Cloudmersive website; this will provide 800 monthly calls across any of our APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet