运行以下项目时,将在设备上生成已编辑的视频。我用音频文件尝试了以下实现并成功了。但是,如果将其作为电影运行,则不会发出错误,但会生成电影,但不会进入该目录 https://github.com/Ryosuke-Hujisawa/My_AVAssetExportSession_AVMutableComposition-2
我的项目中有一个模型。该模型如下 https://github.com/justinlevi/AVAssetExportSession_AVMutableComposition
我成功完成了音频文件。音频处于修剪和编辑状态的目录中。我想编辑视频。虽然我可以编辑视频,但它被编辑并且没有错误发生,但结果在目录中不存在。或者它存在于音频文件状态的目录中,并且不会生成为动画。请帮帮我。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var asset: AVAsset?
@IBAction func exportBtnDidTap(_ sender: AnyObject) {
guard let asset = asset else {
return
}
createAudioFileFromAsset(asset)
}
override func viewDidLoad() {
super.viewDidLoad()
let videoAsset = AVURLAsset(url: Bundle.main.url(forResource: "sample", withExtension: "m4v")!)
let comp = AVMutableComposition()
let videoAssetSourceTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first! as AVAssetTrack
let videoCompositionTrack = comp.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
do {
try videoCompositionTrack.insertTimeRange(
CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(10, 600)),
of: videoAssetSourceTrack,
at: kCMTimeZero)
}catch { print(error) }
asset = comp
}
func deleteFile(_ filePath:URL) {
guard FileManager.default.fileExists(atPath: filePath.path) else {
return
}
do {
try FileManager.default.removeItem(atPath: filePath.path)
}catch{
fatalError("Unable to delete file: \(error) : \(#function).")
}
}
func createAudioFileFromAsset(_ asset: AVAsset){
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory.appendingPathComponent("rendered-audio.m4v")
deleteFile(filePath)
if let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A){
exportSession.canPerformMultiplePassesOverSourceMediaData = true
exportSession.outputURL = filePath
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration)
exportSession.outputFileType = AVFileTypeAppleM4A
exportSession.exportAsynchronously {
_ in
print("finished: \(filePath) : \(exportSession.status.rawValue) ")
}
}
}
}
答案 0 :(得分:0)
我可以使用下面的实现裁剪视频。还更新了github
import UIKit
import AVFoundation
class ViewController: UIViewController {
var asset: AVAsset?
@IBAction func exportBtnDidTap(_ sender: AnyObject) {
guard let asset = asset else {
return
}
createAudioFileFromAsset(asset)
}
override func viewDidLoad() {
super.viewDidLoad()
let videoAsset = AVURLAsset(url: Bundle.main.url(forResource: "sample", withExtension: "m4v")!)
let comp = AVMutableComposition()
let videoAssetSourceTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first! as AVAssetTrack
let videoCompositionTrack = comp.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
do {
try videoCompositionTrack.insertTimeRange(
CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(1, 600)),
of: videoAssetSourceTrack,
at: kCMTimeZero)
}catch { print(error) }
asset = comp
}
func deleteFile(_ filePath:URL) {
guard FileManager.default.fileExists(atPath: filePath.path) else {
return
}
do {
try FileManager.default.removeItem(atPath: filePath.path)
}catch{
fatalError("Unable to delete file: \(error) : \(#function).")
}
}
func createAudioFileFromAsset(_ asset: AVAsset){
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory.appendingPathComponent("rendered-audio.m4v")
deleteFile(filePath)
if let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPreset640x480){
exportSession.canPerformMultiplePassesOverSourceMediaData = true
exportSession.outputURL = filePath
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration)
exportSession.outputFileType = AVFileTypeQuickTimeMovie
exportSession.exportAsynchronously {
_ in
print("finished: \(filePath) : \(exportSession.status.rawValue) ")
}
}
}
}