Resize a Video and Preserve Aspect Ratio in Swift

Cloudmersive
2 min readAug 18, 2021

--

When resizing a video, you must first determine whether you wish to preserve the aspect ratio, which is the ratio of the film’s width to its height. By preserving the aspect ratio of a video, you can ensure that the original vision of the content is not lost in translation. If you choose this option for resizing, the following API can be used in Swift to assist in tailoring the process to your needs while saving you a good deal of manual work.

To call the API function you will need to input the video file (or file URL if over 2 GB) along with your API key into the below example code:

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/resize/preserveAspectRatio")!,timeoutInterval: Double.infinity)
request.addValue("<string>", forHTTPHeaderField: "fileUrl")
request.addValue("<integer>", forHTTPHeaderField: "maxWidth")
request.addValue("<integer>", forHTTPHeaderField: "maxHeight")
request.addValue("<integer>", forHTTPHeaderField: "frameRate")
request.addValue("<integer>", forHTTPHeaderField: "quality")
request.addValue("<string>", forHTTPHeaderField: "extension")
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()

Depending on the file size, the number of API calls that are used and the amount of time the operation takes can vary — the maximum processing time is 25 minutes, which should only occur with very large files.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet