123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- import Foundation
- import AVFoundation
- import ObjectMapper
- import RealmSwift
- class PQEditSectionModel: PQEditBaseModel {
- @objc dynamic var addAutoEffect: Int = 0
- @objc dynamic var sectionDuration: Float64 = 0 {
- didSet {
- BFLog(message: "sectionDuration == \(sectionDuration)")
- }
- }
- @objc dynamic var projectTimelineIn: Float64 = 0
- @objc dynamic var projectTimelineOut: Float64 = 0
-
- @objc dynamic var sectionIndex: Int = 0 {
- didSet {
- BFLog(message: "sectionIndex is \(sectionIndex)")
- }
- }
- @objc dynamic var sectionText: String = "" {
- didSet {
- BFLog(message: "文字发生了改变")
-
- }
- }
- @objc dynamic var sectionTimeline: PQEditSectionTimelineModel? {
- didSet {}
- }
- @objc dynamic var sectionType: String = "normal"
-
- @objc dynamic var sectionExtData: PQEditSectionExtDataModel?
-
- var textIsChane: Bool = false
-
- var isSelected: Int = 0
-
- var enabledSetting: Bool = true
-
- var enableAdd: Bool = true
-
- var selectVoice: PQVoiceModel?
-
- var coverImage: UIImage?
-
- var audioFilePath: String = "" {
- didSet {
- if (sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().count ?? 0) > 0 {
- sectionDuration = allStickerAptDuration()
- } else {
- if audioFilePath.count > 0 {
- let audioAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + audioFilePath), options: nil)
- sectionDuration = Float64(audioAsset.duration.seconds)
- }
- }
- }
- }
-
- var mixEmptyAuidoFilePath: String = ""
- var subTitles = List<PQEditSubTitleModel>()
-
- public var bgmData: PQVoiceModel?
-
- var audioAvatarUrl: String = ""
-
-
- var cacheRecorderFiles: [URL] = Array()
-
- var cacheRecorderCount: Int = 0
-
- var compliteMP3AudioFile: String = ""
-
- var audioPowers: [Int] = Array()
-
-
- var isConverding: Bool = false
- required convenience init?(map _: Map) {
- self.init()
- }
- required init() {
- super.init()
- sectionTimeline = PQEditSectionTimelineModel()
- }
- override func mapping(map: Map) {
- addAutoEffect <- map["addAutoEffect"]
- sectionDuration <- (map["duration"], timeTransform)
- projectTimelineIn <- (map["projectTimelineIn"], timeTransform)
- projectTimelineOut <- (map["projectTimelineOut"], timeTransform)
- sectionText <- map["sectionText"]
- sectionTimeline <- map["sectionTimeline"]
- sectionType <- map["sectionType"]
- if sectionType != "global" {
- sectionIndex <- (map["sectionIndex"], sectionIndexTransform)
- } else {
- sectionIndex <- map["sectionIndex"]
- }
- sectionExtData <- map["sectionExtData"]
-
- audioFilePath <- map["audioFilePath"]
- mixEmptyAuidoFilePath <- map["mixEmptyAuidoFilePath"]
- subTitles <- (map["subTitles"], PQListTransform<PQEditSubTitleModel>())
- uniqueId <- map["uniqueId"]
- }
-
- func allStickerAptDuration() -> Float64 {
- var stickerAptDuration: Float64 = 0
- if sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().count ?? 0 > 0 {
- for sticker in (sectionTimeline!.visionTrack?.getEnableVisionTrackMaterials())! {
- stickerAptDuration = stickerAptDuration + Double(lround(sticker.aptDuration))
- }
- }
- return stickerAptDuration
- }
-
- func allStickerAptDurationNoRound() -> Float64 {
- var stickerAptDuration: Float64 = 0
- if sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().count ?? 0 > 0 {
- for sticker in (sectionTimeline!.visionTrack?.getEnableVisionTrackMaterials())! {
- stickerAptDuration = stickerAptDuration + Double(sticker.aptDuration)
- }
- }
- return stickerAptDuration
- }
-
- func haveSelectVoice() -> Bool {
- BFLog(message: "voice?.avatarUrl \(String(describing: selectVoice?.avatarUrl))")
- return selectVoice?.avatarUrl.count ?? 0 > 0
- }
-
-
- func matrialIsDownloaded() -> Bool {
- var isDownloaded: Bool = true
- for sticker in (sectionTimeline!.visionTrack?.getEnableVisionTrackMaterials())! {
- if sticker.locationPath.count == 0 {
- isDownloaded = false
- break
- }
- }
- return isDownloaded
- }
-
- func addSubtitleMatraislInfo(subtitls: List<PQEditVisionTrackMaterialsModel>) {
- deleteSubtitleMatraislInfo()
-
- sectionTimeline!.visionTrack?.visionTrackMaterials.append(objectsIn: subtitls)
- }
-
- func deleteSubtitleMatraislInfo() {
-
- let otherEnableVision = sectionTimeline!.visionTrack?.getEnableVisionTrackMaterials()
- sectionTimeline!.visionTrack?.visionTrackMaterials = otherEnableVision ?? List<PQEditVisionTrackMaterialsModel>.init()
- }
-
- func haveRes() -> Bool {
- var have: Bool = false
- if subTitles.count > 0 || (sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().count ?? 0) > 0 || (audioFilePath.count > 0 && !audioFilePath.contains("empty")) || sectionTimeline?.visionTrack?.getSubtitleMatraislInfo().count ?? 0 > 0 || sectionText.count > 0 {
- have = true
- }
- return have
- }
-
- func generateEmptyAuido() {
- var stickerTotalDuration: Float64 = 0
-
- if sectionText.count == 0, audioFilePath.count == 0, mixEmptyAuidoFilePath.count == 0 {
- BFLog(message: "section index is \(sectionIndex)")
- if !isEmptyObject(object: sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials()) {
- for sticker in sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials() {
- if sticker.timelineOut == 0, sticker.timelineIn == 0 {
- stickerTotalDuration = stickerTotalDuration + (sticker.out - sticker.model_in)
- } else {
- stickerTotalDuration = stickerTotalDuration + (sticker.timelineOut - sticker.timelineIn)
- }
- }
- }
-
- let tool = PQCreateEmptyWAV(sampleRate: 44100,
- channel: 1,
- duration: stickerTotalDuration,
- bit: 16)
- let timeInterval: TimeInterval = Date().timeIntervalSince1970
- var documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
- documentPath.append("/\(timeInterval).wav")
- tool.createEmptyWAVFile(url: URL(fileURLWithPath: documentPath))
-
- audioFilePath = documentPath
- sectionDuration = CMTimeGetSeconds(AVURLAsset(url: URL(fileURLWithPath: documentPath), options: nil).duration)
- BFLog(message: " 生成的空声音 \(String(describing: documentPath)) 时长 \(String(describing: sectionDuration))")
-
- }
- }
-
- func getInputSubtitle() -> String {
- var text: String = ""
- if sectionTimeline?.visionTrack?.getSubtitleMatraislInfo() != nil {
- for subTitleModel in sectionTimeline!.visionTrack!.getSubtitleMatraislInfo() {
- if subTitleModel.subtitleInfo?.text.count ?? 0 > 0 {
- text = text + (subTitleModel.subtitleInfo?.text ?? "") + "\n"
- }
- }
- }
- return text
- }
-
- func deleteCacheAudioFiles(isDeleteMergeFile: Bool = false) {
- let fileManger = FileManager.default
-
-
- for url in cacheRecorderFiles {
- if fileManger.fileExists(atPath: url.relativePath) {
- do {
- try fileManger.removeItem(atPath: url.relativePath)
- print("\(url.relativePath)Success to remove file.")
- } catch {
- print("Failed to remove file.")
- }
- }
- }
- cacheRecorderFiles.removeAll()
-
- if isDeleteMergeFile {
- if fileManger.fileExists(atPath: compliteMP3AudioFile) {
- do {
- try fileManger.removeItem(atPath: compliteMP3AudioFile)
- print("\(compliteMP3AudioFile)Success to remove file.")
- } catch {
- print("Failed to remove file.")
- }
- }
- }
- compliteMP3AudioFile = ""
- audioPowers = []
- }
- }
|