我正在使用文件管理器保存视频:
switch _captureState {
case .start:
print("Set up recorder")
// Set up recorder
// let dateFormat = DateFormatter()
// dateFormat.dateFormat = "MMM dd, yyyy 'at' hh:MM a"
let creationDate = "\(Date().millisecondsSince1970)" //dateFormat.string(from: Date())
let projectInputName = projectName.replacingOccurrences(of: " ", with: "_")
let sessionsNameInput = sessionsName.replacingOccurrences(of: " ", with: "_")
let fullFilename = "ProjectName_\(projectInputName)_clientName_\(sessionsNameInput)_creationDate_\(creationDate)"
let videoPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("\(fullFilename).mov")
print("videoPath: \(videoPath)")
let writer = try! AVAssetWriter(outputURL: videoPath, fileType: .mov)
let settings = _videoOutput!.recommendedVideoSettingsForAssetWriter(writingTo: .mov)
let input = AVAssetWriterInput(mediaType: .video, outputSettings: settings) // [AVVideoCodecKey: AVVideoCodecType.h264, AVVideoWidthKey: 1920, AVVideoHeightKey: 1080])
// let audioSettings = audioOutput //_videoOutput!.recommendedVideoSettingsForAssetWriter(writingTo: .mov)
let audioSettings : Dictionary = [
AVFormatIDKey : kAudioFormatMPEG4AAC as AnyObject,
AVNumberOfChannelsKey : 2,
AVSampleRateKey : NSNumber(value: 48000.0 )
] as [String: Any]
let audioInput = AVAssetWriterInput(mediaType: .audio, outputSettings: audioSettings )
input.mediaTimeScale = CMTimeScale(bitPattern: 600)
input.expectsMediaDataInRealTime = true
audioInput.expectsMediaDataInRealTime = true
input.transform = CGAffineTransform(rotationAngle: .pi/2)
let adapter = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: input, sourcePixelBufferAttributes: nil)
if writer.canAdd(input) {
writer.add(input)
}
if writer.canAdd(audioInput) {
writer.add(audioInput)
NSLog("audio input added")
}
//
writer.startWriting()
writer.startSession(atSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer))
_assetWriter = writer
_assetWriterInput = input
_assetWriterInputAudio = audioInput
_adpater = adapter
_captureState = .capturing
_time = timestamp
case .capturing:
// print(".capturing")
if _assetWriterInput?.isReadyForMoreMediaData == true , output == _videoOutput {
if !hasWrittenFirstVideoFrame {
// _assetWriter?.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(sampleBuffer))
hasWrittenFirstVideoFrame = true }
let time = timestamp // - _time //CMTime(seconds: (timestamp - _time).seconds, preferredTimescale: CMTimeScale(600))
_adpater?.append(CMSampleBufferGetImageBuffer(sampleBuffer)!, withPresentationTime: time)
}
//
// if output == _videoOutput {
// if _assetWriterInput?.isReadyForMoreMediaData == true {
//// let time = CMTime(seconds: timestamp - _time, preferredTimescale: CMTimeScale(600))
//// _adpater?.append(CMSampleBufferGetImageBuffer(sampleBuffer)!, withPresentationTime: time)
// print(".capturing")
// let time = CMTime(seconds: timestamp - _time, preferredTimescale: CMTimeScale(600))
// _adpater?.append(CMSampleBufferGetImageBuffer(sampleBuffer)!, withPresentationTime: time)
// }
// }
else if output == _audioOutput , _assetWriterInputAudio?.isReadyForMoreMediaData == true , hasWrittenFirstVideoFrame {
if _assetWriterInputAudio?.isReadyForMoreMediaData == true {
// if !mute{
if mute {
let vc = buffer()
vc.muteAudio(in: sampleBuffer)
}
_assetWriterInputAudio?.append(sampleBuffer)
// }
}
}
break
case .end:
print(".end")
// BackgroundTask.run(application: UIApplication.shared)
// { backgroundTask in
// print(".end")
guard _assetWriterInput?.isReadyForMoreMediaData == true , _assetWriterInputAudio?.isReadyForMoreMediaData == true, _assetWriter!.status != .failed else { return }
_assetWriterInput?.markAsFinished()
_assetWriterInputAudio?.markAsFinished()
let savingAssetWriter = self._assetWriter
self._assetWriter = nil
savingAssetWriter?.finishWriting { [weak self] in
print("capture state idle")
self?._captureState = .idle
self?._assetWriter = nil
self?._assetWriterInput = nil
self?._assetWriterInputAudio = nil
}
break
default:
break
}
这是保存后的文件路径: “/private/var/mobile/Containers/Data/Application/17DCBC0A-5FD1-4EE0-A30E-FF6E40AAA91C/Documents/ProjectName_Testing_With_Files_Version_2_clientName_Scale_Capacity_creationDate_1723149423479.mov”
我的目标是编辑Scale_Capacity(在clientName之后命名,但留下_creationDate_1723149423479.mov并保存回我的文件
例如,将名为“txt”的文件重命名为“newname.txt”。 txt”,您将运行以下命令