Transcode Media
Media files can also be transcoded without using UI.
This example stitches two AVAssets named “test.mov” and “test2.mp4” into a single 60 fps mp4 file with H.264 video encoding and stereo AAC audio encoding.
Additionally, a Saturation filter is applied to the first segment (asset), and a Pixellate filter is applied to the second segment.
import PixelSDK
import AVFoundation
let asset1 = AVAsset(url: Bundle.main.url(forResource: "test", withExtension: "mov")!)
let asset2 = AVAsset(url: Bundle.main.url(forResource: "test2", withExtension: "mp4")!)
let _ = Session(assets: [asset1, asset2], sessionReady: { (session, error) in
guard let session = session,
let video = session.video else {
print("Unable to create session: \(error!)")
return
}
// Mark the session as transient so it does not persist on disk/appear in the users drafts
session.isTransient = true
// Set the video frame rate to 60 fps
video.frameDuration = CMTime(value: 1, timescale: 60)
// Apply a Saturation filter to the first segment
let saturationFilter = SessionFilterSaturation()
saturationFilter.normalizedIntensity = 0.2
video.videoSegments[0].filters = [saturationFilter]
// Apply a Pixellate filter to the second segment
let pixellateFilter = SessionFilterPixellate()
video.videoSegments[1].filters = [pixellateFilter]
// Write to an MP4 file with H.264 video encoding and stereo AAC audio encoding
var acl = AudioChannelLayout()
memset(&acl, 0, MemoryLayout<AudioChannelLayout>.size)
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo
let audioEncodingSettings: [String: Any] = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: AVAudioSession.sharedInstance().sampleRate,
AVChannelLayoutKey: NSData(bytes:&acl, length:MemoryLayout<AudioChannelLayout>.size),
AVEncoderBitRateKey: 96000
]
let videoEncodingSettings: [String: Any] = [
AVVideoCompressionPropertiesKey: [
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCABAC],
AVVideoCodecKey: AVVideoCodecType.h264
]
VideoExporter.shared.export(video: video,
fileType: .mp4,
videoEncodingSettings: videoEncodingSettings,
audioEncodingSettings: audioEncodingSettings,
progress: { (progress) in
print("Transcode progress: \(progress)")
}, completion: { (error) in
if let error = error {
print("Unable to transcode video: \(error)")
return
}
print("Finished video transcode at URL: \(video.exportedVideoURL)")
})
})
After your transcode has completed, you may move, copy or delete the file found at the video.exportedVideoURL
.
There are many combinations of encoding settings you can provide. They must conform to the specifications set forth in AVFoundations AVVideoSettings.h and AVAudioSettings.h headers. There is also a list of available codecs. Keep in mind each codec may have different requirements for the settings you provide.