-
-
Save yeelone/aac9bbf652aaece4c2d50d4a7341a8cc to your computer and use it in GitHub Desktop.
replaykit test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import AVFoundation | |
class VideoWriter { | |
var avAssetWriter: AVAssetWriter | |
var avAssetWriterInput: AVAssetWriterInput | |
var url:URL | |
var name:String | |
init(){ | |
print("init writer") | |
let audioSettings = [ | |
AVFormatIDKey : kAudioFormatMPEG4AAC, | |
AVNumberOfChannelsKey : 2, | |
AVSampleRateKey : 44100.0, | |
AVEncoderBitRateKey: 64000 | |
] as [String : Any] | |
avAssetWriterInput = AVAssetWriterInput(mediaType: AVMediaType.audio, | |
outputSettings: audioSettings) | |
avAssetWriterInput.expectsMediaDataInRealTime = true | |
do { | |
let directory = try VideoWriter.directoryForNewVideoInAppGroup() | |
name = UUID.init().uuidString.appending(".mp4") | |
// url = directory.appendingPathComponent(name) | |
url = directory.appendingPathComponent("-babysound-12345.mp4") | |
avAssetWriter = try AVAssetWriter(url: url, fileType: AVFileType.mp4 ) | |
if avAssetWriter.canAdd(avAssetWriterInput) { | |
avAssetWriter.add(avAssetWriterInput) } | |
avAssetWriter.movieFragmentInterval = CMTime.invalid | |
} catch { | |
fatalError("Could not initialize avAssetWriter \(error)") | |
} | |
} | |
func write(sampleBuffer buffer: CMSampleBuffer, completionHandler : (_: Bool,_: String) -> ()) { | |
if avAssetWriter.status == .unknown { | |
if avAssetWriter.startWriting() { | |
print("starwriting") | |
avAssetWriter.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(buffer)) | |
}else { | |
print("cannot start writing") | |
print(avAssetWriter.error!) | |
print("cannot start writing ////////") | |
return | |
} | |
} | |
if avAssetWriterInput.isReadyForMoreMediaData { | |
avAssetWriterInput.append(buffer) | |
} | |
completionHandler(true,name) | |
} | |
func stopWriting(completionHandler handler: @escaping (AVAssetWriter.Status) -> Void) { | |
avAssetWriter.finishWriting { | |
handler(self.avAssetWriter.status) | |
print(self.avAssetWriter.status) | |
} | |
} | |
static func directoryForNewVideo() throws -> URL { | |
let videoDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("videos") | |
// let formatter = DateFormatter() | |
// formatter.dateFormat = "dd-MM-yyyy" | |
// let dateDir = videoDir?.appendingPathComponent(formatter.string(from:Date())) | |
try FileManager.default.createDirectory(atPath: (videoDir?.path)!, withIntermediateDirectories: true, attributes: nil) | |
return videoDir! | |
} | |
static func directoryForNewVideoInAppGroup() throws -> URL { | |
let fileManager = FileManager.default | |
// var directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.tdongli") | |
let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.tdongli") | |
let newDirectory = directory?.appendingPathComponent("audio") | |
do { | |
try fileManager.removeItem(atPath: newDirectory!.path) | |
}catch{ | |
// | |
} | |
do { | |
try fileManager.createDirectory(at: newDirectory!, withIntermediateDirectories: false, attributes: nil) | |
}catch{ | |
print("directoryForNewVideoInAppGroup Unexpected error: \(error).") | |
} | |
return newDirectory!; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment