SwiftVideoGenerator 1.4.0

SwiftVideoGenerator 1.4.0

Dev Labs BG维护。



swift-video-generator-logo

Platform CocoaPods License Twitter URL Website

此库提供了一种简单的方法,将图像和音频合并成视频或合并多个视频成一个。

功能

  • 从单个图像和音频文件创建视频
  • 从多个图像/音频对创建视频
  • 从单个音频文件和多个图像创建视频
  • 从单个图像(无音频)创建视频
  • 从多个图像(无音频)创建视频
  • 将多个视频合并成一个
  • 反转视频片段
  • 按时间范围分割视频
  • 将单个视频与单个音频合并

支持的视频格式

  • mov(仅当合并视频时)
  • m4v
  • mp4(仅当合并视频时)

要求

  • iOS 9.0或更高版本
  • Xcode 8.3或更高版本
  • Swift 3.2或更高版本

通信

  • 如果您需要帮助或有一般性问题,您可以在@guardians_devil@devlabsbg上找到我。或者,在Twitter上标记#swift-video-generator。
  • 如果您发现了一个错误,请提交一个issue。
  • 如果您有一个功能请求,请提交一个issue。
  • 如果您想贡献,请提交一个pull request。

贡献者

  • ibhavin - 版本1.0.8从单个音频和多个图片创建视频
  • SteliyanH - 版本1.1.0逆序视频剪辑中的音频

安装

CocoaPods

CocoaPods 是 Xcode 的依赖管理器。您可以在终端中运行以下命令来安装它。

$ gem install cocoapods

要使用 CocoaPods 在您的 Xcode 项目中使用 Swift Video Generator,请在 Podfile 中添加它。

platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftVideoGenerator'
end

Swift 包管理器

要将库作为包依赖项添加到您的 Xcode 项目中,请选择文件 > Swift 包 > 添加包依赖项,并输入其存储库网址 [email protected]:dev-labs-bg/swift-video-generator.git

手动

如果您不想使用依赖管理器,您可以手动集成 SwiftVideoGenerator。

下载以下文件:

视频生成类

您需要 VideoGenerator.swift 和 Error 和 Extensions 文件夹中的文件。

将文件添加到项目中
  • 在Xcode中打开你的项目
  • 将你下载的文件添加到项目导航器。你可以创建自己的文件夹或者保留文件的原有结构。
  • 完成!你现在可以开始制作视频了。

使用方法

如果你使用Cocoapods安装了SwiftVideoGenerator,你需要导入该模块

import SwiftVideoGenerator

对于生成视频的类型.single.multiple,输出视频文件格式是m4v。

从单音频和图片文件创建视频

if let audioURL4 = Bundle.main.url(forResource: Audio4 , withExtension: Mp3Extension) {
  LoadingView.lockView()

  VideoGenerator.fileName = SingleMovieFileName
  VideoGenerator.shouldOptimiseImageForVideo = true

  VideoGenerator.current.generate(withImages: [#imageLiteral(resourceName: "image4")], andAudios: [audioURL4], andType: .single, { (progress) in
    print(progress)
  }, success: { (url) in
    LoadingView.unlockView()
    print(url)
    self.createAlertView(message: self.FinishedSingleTypeVideoGeneration)
  }, failure: { (error) in
    LoadingView.unlockView()
    print(error)
    self.createAlertView(message: error.localizedDescription)
  })
} else {
  self.createAlertView(message: MissingResourceFiles)
}

使用生成类型.single,你可以从一对音频和图片中创建一个视频。

如果你将audio数组留空并实现videoDurationInSeconds,生成的视频将只包含图片,不包含音频。

示例视频 - 单类型生成

scaleWidth属性将图片缩放至所需的大小。仅在生成类型为.single的视频时使用。

fileName属性为输出文件设置名称。

videoBackgroundColor属性在缩放图片时使用。当图片缩放到一个比视频画框小的帧时,图片周围会留下空白空间。你可以使用视频背景颜色属性设置该空间的颜色。如果你不指定强项宽度,图片会被缩放(保持长宽比)以填满整个视频画框。

maxVideoLengthInSeconds属性用于设置视频的最大长度。如果视频长度超过此值,将会在指定的时间处截断。(注意,这不会为了适应时间而缩放音频。)

shouldOptimiseImageForVideo属性用于根据图像的方向将输入到生成器中的图像优化到为视频最适合的比例。

为无音频生成视频设置videoDurationInSeconds

从多个图像音频对创建视频

if let audioURL1 = Bundle.main.url(forResource: Audio1, withExtension: Mp3Extension), let audioURL2 = Bundle.main.url(forResource: Audio2, withExtension: Mp3Extension), let audioURL3 = Bundle.main.url(forResource: Audio3, withExtension: Mp3Extension) {
  LoadingView.lockView()

  VideoGenerator.fileName = MultipleMovieFileName
  VideoGenerator.videoBackgroundColor = .red
  VideoGenerator.videoImageWidthForMultipleVideoGeneration = 2000

  VideoGenerator.current.generate(withImages: [#imageLiteral(resourceName: "image1"), #imageLiteral(resourceName: "image2"), #imageLiteral(resourceName: "image3")], andAudios: [audioURL1, audioURL2, audioURL3], andType: .multiple, { (progress) in
    print(progress)
  }, success: { (url) in
    LoadingView.unlockView()
    print(url)
    self.createAlertView(message: self.FnishedMultipleVideoGeneration)
  }, failure: { (error) in
    LoadingView.unlockView()
    print(error)
    self.createAlertView(message: error.localizedDescription)
  })
} else {
  self.createAlertView(message: MissingAudioFiles)
}

使用类型 .multiple,您可以创建一个包含多个图像/音频对的视频。最终的视频将排队处理多个视频,每个视频从图像数组中取一个图像以及相应索引的音频数组元素,然后用它创建一个视频,并将其附加到最终的视频中。

如果您将 audio 数组留空,并实现 videoDurationInSeconds,生成的视频将只包含图像,没有音频。

然后,下一对音频和图像将被制成视频,并附加到第一个视频之后。这将继续进行,直到将所有图像/音频对附加完毕。如果图像/音频对不是相同的计数,将不使用多余的音频(图像)。

fileNamevideoBackgroundColor 属性的使用方式与 .single 类型相同。

videoImageWidthForMultipleVideoGeneration 属性用于设置图像在被合并到音频文件并生成视频之前会被缩放的特定宽度。默认值是 800。

示例视频 - 多种类型生成视频

从多个图像和单个音频制作视频

if let audioURL1 = Bundle.main.url(forResource: Audio1, withExtension: Mp3Extension) {
  LoadingView.lockView()

  VideoGenerator.fileName = MultipleSingleMovieFileName
  VideoGenerator.shouldOptimiseImageForVideo = true

  VideoGenerator.current.generate(withImages: [#imageLiteral(resourceName: "image1"), #imageLiteral(resourceName: "image2"), #imageLiteral(resourceName: "image3"), #imageLiteral(resourceName: "image4")], andAudios: [audioURL1], andType: .singleAudioMultipleImage, { (progress) in
    print(progress)
  }, success: { (url) in
    LoadingView.unlockView()
    print(url)
    self.createAlertView(message: self.FnishedMultipleVideoGeneration)
  }, failure: { (error) in
    LoadingView.unlockView()
    print(error)
    self.createAlertView(message: error.localizedDescription)
  })
} else {
  self.createAlertView(message: MissingAudioFiles)
}

使用类型 .singleAudioMultipleImage,您可以创建一个将多个图像和单个音频组合在一起的视频。最终的视频将在单个音频的时间线上均匀排列多个图像。

示例视频 - 多图像/单音频生成视频

将多个视频合并为单个视频

if let videoURL1 = Bundle.main.url(forResource: Video1, withExtension: MOVExtension), let videoURL2 = Bundle.main.url(forResource: PortraitVideo, withExtension: Mp4Extension) {
  LoadingView.lockView()
  VideoGenerator.presetName = AVAssetExportPresetPassthrough
  VideoGenerator.fileName = MergedMovieFileName
  
  VideoGenerator.mergeMovies(videoURLs: [videoURL1, videoURL2], success: { (videoURL) in
    LoadingView.unlockView()
    self.createAlertView(message: self.FinishedMergingVideos)
    print(videoURL)
  }) { (error) in
    LoadingView.unlockView()
    print(error)
    self.createAlertView(message: error.localizedDescription)
  }
} else {
  self.createAlertView(message: MissingVideoFiles)
}

您可以提供本地资源文件以及存储在设备上(例如,在文档文件夹中)的文件的 URL。

截至目前,合并的视频保留了最后提供的视频资源的所有 preferredTransformations(如镜像或方向)。更多功能将在未来的功能实现中带来。

示例视频 - 合并视频

反转视频片段

if let videoURL1 = Bundle.main.url(forResource: Video2, withExtension: MovExtension) {
  LoadingView.lockView()
  VideoGenerator.fileName = ReversedMovieFileName
  
  VideoGenerator.current.reverseVideo(fromVideo: videoURL1, success: { (videoURL) in
    LoadingView.unlockView()
    self.createAlertView(message: self.FinishReversingVideo)
    print(videoURL)
  }, failure: { (error) in
    LoadingView.unlockView()
    print(error)
    self.createAlertView(message: error.localizedDescription)
  })
} else {
  self.createAlertView(message: self.MissingVideoFiles)
}

您需要提供文件的 URL,可选地为反转的视频文件提供新名称。反转完成的视频没有音频。

示例视频 - 反转视频

分割视频片段

if let videoURL1 = Bundle.main.url(forResource: Video1, withExtension: MOVExtension) {
  LoadingView.lockView()

  VideoGenerator.fileName = SplitMovieFileName
  
  VideoGenerator.current.splitVideo(withURL: videoURL1, atStartTime: 10, andEndTime: 40, success: { (url) in
    LoadingView.unlockView()
    print(url)
    self.createAlertView(message: self.FinishSplittingVideo)
  }, failure: { (error) in
    LoadingView.unlockView()
    print(error)
    self.createAlertView(message: error.localizedDescription)
  })
} else {
  self.createAlertView(message: self.MissingVideoFiles)
}

您需要提供文件的URL,以及可选的新分视频文件名称。《strong>atStartTime 和 andEndTime 属性标记时间范围的开始和结束(单位:秒)。

示例视频 - 分割视频

将视频片段与自定义音频合并

if let videoURL2 = Bundle.main.url(forResource: Video2, withExtension: MOVExtension), let audioURL2 = Bundle.main.url(forResource: Audio2, withExtension: Mp3Extension) {
  LoadingView.lockView()

  VideoGenerator.fileName = NewAudioMovieFileName
  
  VideoGenerator.current.mergeVideoWithAudio(videoUrl: videoURL2, audioUrl: audioURL2, success: { (url) in
    LoadingView.unlockView()
    print(url)
    self.createAlertView(message: self.FinishMergingVideoWithAudio)
  }) { (error) in
    LoadingView.unlockView()
    print(error)
    self.createAlertView(message: error.localizedDescription)
  }
} else {
  self.createAlertView(message: self.MissingVideoFiles)
}

您需要提供视频和音频的URL,以及可选的新生成视频文件名称。

示例视频 - 与新音频合并的视频

以下应用已使用

鸣谢

SwiftVideoGenerator是由Dev Labs创建和维护的。您可以在@devlabsbgdevlabs.bg找到我们。

许可

SwiftVideoGenerator遵循MIT许可。有关详细信息,请参阅许可文件