SwiftVideoCreator
[](https://travis-ci.org/Lexie Kemp/SwiftVideoCreator)
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install。
要求
Swift 5.0
安装
SwiftVideoCreator 通过 CocoaPods 提供。您需要已安装 CocoaPods。要安装 SwiftVideoCreator,将以下行添加到您的 Podfile 中
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftVideoCreator'
end
接下来,在您的项目目录中通过命令行运行 pod install。
使用方法
导入Pod
import SwiftVideoCreator
获取视频URL、音频URL和CGImage图像。例如,如果视频和音频存储在项目中,图像存储在资产中
guard let videoPath = Bundle.main.path(forResource: "myVideo", ofType:"mov") else {
print("myVideo.mov not found")
return
}
let videoUrl = URL(fileURLWithPath: videoPath)
guard let audioPath = Bundle.main.path(forResource: "myAudio", ofType:"mp3") else {
print("myAudio.mp3 not found")
return
}
let audioUrl = URL(fileURLWithPath: audioPath)
guard let backgroundImage = UIImage(named: "myBackgroundImage") else {
print("Background Image not found in Assets")
return
}
guard let cgBackgroundImage = backgroundImage.cgImage else {
print("could not convert background image to cgImage")
return
}
调用createVideo函数,将合成的视频保存到文档目录并返回URL。视频大小是保存的视频合成的尺寸。视频尺寸是图像上方的视频尺寸。原点是左下角
SwiftVideoCreator.current.createVideo(
fileName: "exampleVideo",
compositionSize: CGSize(width: 360, height: 360),
videoUrl: waveUrl,
videoSize: CGSize(width: 360, height: 90),
videoOrigin: CGPoint(x: 0, y: 100),
videoOpacity: 0.7,
audioUrl: audioUrl,
image: cgBackgroundImage,
imageOrigin: CGPoint(x: 0, y: 0),
imageSize: CGSize(width: 360, height: 360),
imageOpacity: 1.0,
success: { url in
print(url)
},
failure: { error in
print("video creation failed with error \(error)")
})
如果您想在视频播放器中打开视频,请使用以下函数
func playVideo(videoUrl: URL) {
let player = AVPlayer(url: videoUrl)
let playerVC = AVPlayerViewController()
playerVC.player = player
present(playerVC, animated: true) {
player.play()
}
}
作者
Lexie Kemp, [email protected]
许可证
SwiftVideoCreator 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。