Swift 为什么我的avfoundationswifft3不工作
运行以下项目时,将在设备上生成已编辑的视频。我用音频文件尝试了以下实现,并成功了。但是,如果将其作为电影运行,则不会发出任何错误,但会生成一部电影,并且不会进入目录 我的项目中有一个模型。模型如下 我成功地创建了音频文件。音频在目录中处于修剪和编辑状态。我想编辑视频。虽然我可以编辑视频,但它已编辑且未发生错误,但结果不存在于目录中。或者它以音频文件的状态存在于目录中,并且不作为动画生成。请帮帮我Swift 为什么我的avfoundationswifft3不工作,swift,xcode,avfoundation,Swift,Xcode,Avfoundation,运行以下项目时,将在设备上生成已编辑的视频。我用音频文件尝试了以下实现,并成功了。但是,如果将其作为电影运行,则不会发出任何错误,但会生成一部电影,并且不会进入目录 我的项目中有一个模型。模型如下 我成功地创建了音频文件。音频在目录中处于修剪和编辑状态。我想编辑视频。虽然我可以编辑视频,但它已编辑且未发生错误,但结果不存在于目录中。或者它以音频文件的状态存在于目录中,并且不作为动画生成。请帮帮我 import UIKit import AVFoundation class ViewCont
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) ")
}
}
}
}
我可以用下面的实现裁剪视频。还更新了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) ")
}
}
}
}
我可以用下面的实现裁剪视频。还更新了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) ")
}
}
}
}
我尝试exportSession.outputFileType=AVFileTypeAppleM4A->exportSession.outputFileType=AvFileTypeApplem4vb,但这是错误->原因:“输出文件类型无效”***第一次抛出调用堆栈:尝试AVFileTypeMPEG4?我尝试->如果让exportSession=AVAssetExportSession(资产:资产,预设名称:AvassetExportExpertExperte640x480){我成功了!!!!!!!!!!!!!exportSession.outputFileType=AVFileTypeAppleM4V->exportSession.outputFileType=AVFileTypeQuickTimeMovie❤️我尝试exportSession.outputFileType=AVFileTypeAppleM4A->exportSession.outputFileType=AvFileTypeApplem4vb,但这是错误->原因:“输出文件类型无效”***第一次抛出调用堆栈:尝试AVFileTypeMPEG4?我尝试->如果让exportSession=AVAssetExportSession(资产:资产,预设名称:AvassetExportExpertExperte640x480){我成功了!!!!!!!!!!!!!exportSession.outputFileType=AVFileTypeAppleM4V->exportSession.outputFileType=AVFileTypeQuickTimeMovie❤️