gpt4 book ai didi

ios - Swift 3 录制视频 - mp4 文件中缺少音频

转载 作者:行者123 更新时间:2023-12-02 03:11:33 24 4
gpt4 key购买 nike

此代码生成一个缺少音频的本地 mp4 文件。如果我在 QuickTime 播放器检查器中查看此文件,它具有 H264 格式,但没有 AAC 音频。如果我在应用程序中播放该文件,则没有声音。

最奇怪的是,我可以将相同的文件作为多部分文档上传到服务器,这很好。我可以从服务器下载该文件,它有 AAC 音频数据。

class VideoViewController: UIViewController, AVCaptureFileOutputRecordingDelegate
{
var captureDevice : AVCaptureDevice! // check capture device availability
var videoInput:AVCaptureDeviceInput?
let captureSession = AVCaptureSession() // to create capture session
var previewLayer : AVCaptureVideoPreviewLayer? // to add video inside container
var videoFileOutput:AVCaptureMovieFileOutput!
var audioDevice:AVCaptureDevice?
var audioInput:AVCaptureDeviceInput?

var playerController = AVPlayerViewController()

@IBOutlet weak var videoView: UIView!

override func viewDidLoad()
{
super.viewDidLoad()
}

override func viewDidDisappear(_ animated: Bool)
{
shutDown()
}

func isVideoSetup() -> Bool
{
if(captureDevice == nil)
{
return false
}

return true
}

func setupCamera()
{

print("setupCamera")

audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
print("Audio capture device found")

//Do any additional setup after loading the view, typically from a nib.
// you have 3 option High quality recording, Medium quality recording and Low quality recording
captureSession.sessionPreset = AVCaptureSessionPresetLow

let deviceDescoverySession = AVCaptureDeviceDiscoverySession.init(deviceTypes: [AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)

if(deviceDescoverySession != nil)
{
for device in deviceDescoverySession!.devices
{
if device.position == AVCaptureDevicePosition.front
{
print("Video capture device found")
captureDevice = device
setupCamera2()
return
}
}
}

print("Capture device not found")
fatalError(errorMsg:"Camera was not found.")

}

//Configuring & Initializing the camera
func setupCamera2()
{
print("setupCamera2")

do {
audioInput = try AVCaptureDeviceInput(device: audioDevice)
print("audio input created")

} catch {
print("Unable to add audio device to the recording.")
fatalError(errorMsg:"Unable to access audio device.")
return
}



if let device = captureDevice {
do{
try device.lockForConfiguration()

}catch{
print("error")
}
}

let err : NSError? = nil

do{
videoInput = try AVCaptureDeviceInput(device: captureDevice)
captureSession.addInput(videoInput)
}catch
{
print("error")
fatalError(errorMsg:"Unknown error.")
return
}

if err != nil
{
print("error: \(String(describing: err?.localizedDescription))")
fatalError(errorMsg:String(describing: err?.localizedDescription))
return
}

self.captureSession.addInput(audioInput)
print("added audio device")

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
// thumbnail is a ui container, your camera shows inside this container
previewLayer?.frame = videoView.layer.bounds
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
videoView.layer.addSublayer(previewLayer!)
captureSession.startRunning()
captureDevice?.unlockForConfiguration()
}



func startRecording()
{
if(captureSession.outputs.count > 0)
//if(captureSession.canAddOutput(videoFileOutput))
{
print("Resetting inputs")
captureSession.removeInput(audioInput)
captureSession.removeInput(videoInput)
captureSession.removeOutput(videoFileOutput)
setupCamera2()
}

videoFileOutput = AVCaptureMovieFileOutput()
videoFileOutput.movieFragmentInterval = CMTime(seconds: 1, preferredTimescale: 30)

captureSession.addOutput(videoFileOutput)

do
{
try FileManager.default.removeItem(at: dataMgr.videoFileURL)
}
catch
{
}

let recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
videoFileOutput.startRecording(toOutputFileURL: dataMgr.videoFileURL, recordingDelegate: recordingDelegate)
}

func stopRecording()
{
//To end recording just call this function
videoFileOutput.stopRecording()

}


func shutDown()
{
if(captureSession.outputs.count > 0)
{
captureSession.removeInput(audioInput)
captureSession.removeInput(videoInput)
captureSession.removeOutput(videoFileOutput)
}

captureSession.stopRunning()
}


func playVideo()
{
let item = AVPlayerItem(url: dataMgr.videoFileURL)
let player = AVPlayer(playerItem: item)
playerController = AVPlayerViewController()
playerController.player = player
playerController.view.frame = CGRect(x:videoView.frame.origin.x, y:videoView.frame.origin.y, width: videoView.frame.width, height: videoView.frame.height)
self.addChildViewController(playerController)
videoView.addSubview(playerController.view)
}

func stopPlaying()
{
//playerController.view.removeSubviews()
playerController.removeFromParentViewController()
videoView.removeSubviews()
utils.removeAllSubviews(vw: videoView)
}


@available(iOS 4.0, *)
public func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!)
{
print("capture did finish")
print(captureOutput);
print(outputFileURL);
print("size of file=" + String(utils.getFileSize(fileURL: outputFileURL)))
}

func fatalError(errorMsg:String)
{
let alert = UIAlertController(title: "Error", message: errorMsg, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
app.switchScreens(newscreen:"StartViewController")

}))
self.present(alert, animated: true, completion: nil)

}
}

最佳答案

这太疯狂了,但我通过更改保存的文件名解决了这个问题

let videoFileName = "mysavefile.mp4";

let videoFileName = "mysavefile.mov";

关于ios - Swift 3 录制视频 - mp4 文件中缺少音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45043496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com