我尝试使用以下代码从iphone相机保存视频文件:
class Camera {
...
private func loadDeviceInput() {
let devices = AVCaptureDevice.devices() as! [AVCaptureDevice]
for device in devices {
if device.hasMediaType(AVMediaTypeVideo) {
if device.position == .Back {
_deviceBack = device
_inputBack = try? AVCaptureDeviceInput(device: device)
}
else if device.position == .Front {
_deviceFront = device
_inputFront = try? AVCaptureDeviceInput(device: device)
}
}
else if device.hasMediaType(AVMediaTypeAudio) {
_inputAudio = try? AVCaptureDeviceInput(device: device)
}
}
}
private func loadDeviceOutput() {
_outputStill = AVCaptureStillImageOutput()
_outputStill?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if _session.canAddOutput(_outputStill) {
_session.addOutput(_outputStill)
}
_outputAudio = AVCaptureAudioDataOutput()
if _session.canAddOutput(_outputAudio) {
_session.addOutput(_outputAudio)
}
_outputVideo = AVCaptureMovieFileOutput()
_outputVideo?.movieFragmentInterval = CMTimeMake(1, 1) // Save file header every second
if _session.canAddOutput(_outputVideo) {
_session.addOutput(_outputVideo)
}
}
func startVideoCapture(writeTo file: NSURL, startedBlock: JrmgxNSURLResultBlock, stopedBlock: JrmgxNSURLResultBlock) {
isSessionRunning()
prepareForVideoCapture()
_startVideoCaptureBlock = startedBlock
_stopVideoCaptureBlock = stopedBlock
if _outputVideo?.connectionWithMediaType(AVMediaTypeVideo)?.active ?? false == true {
_outputVideo?.startRecordingToOutputFileURL(file, recordingDelegate: self)
}
else {
// ERROR
}
}
...
}
到目前为止这是有效的,但是当我使用AVPlayer播放视频时,没有播放声音。如果我用Quiktime尝试视频,同样的问题,没有声音。但是当我用VLC打开它时,声音会播放。
作为参考,这是我播放视频的方式,该视频在没有声音的情况下运行:
player = AVPlayer(URL: v)
player.actionAtItemEnd = .None
player.muted = false
player.volume = 1
player.play()
我使用ffprone
检查了视频:
$ ffprobe test4.mp4
ffprobe version 2.8.1
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test4.mp4':
Metadata:
major_brand : qt
minor_version : 0
compatible_brands: qt
creation_time : 2016-01-21 13:13:55
Duration: 00:00:06.37, start: 0.000000, bitrate: 818 kb/s
Stream #0:0(und): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt709/bt709), 480x360, 741 kb/s, 30.01 fps, 30 tbr, 600 tbn, 1200 tbc (default)
Metadata:
rotate : 90
creation_time : 2016-01-21 13:13:55
handler_name : Core Media Video
Side data:
displaymatrix: rotation of -90.00 degrees
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 62 kb/s (default)
Metadata:
creation_time : 2016-01-21 13:13:55
handler_name : Core Media Audio
我不知道出了什么问题, 你能救我吗?
谢谢
答案 0 :(得分:5)
mp4和mov文件非常相似,但有细微差别。我认为VLC只是意识到文件实际上是一个.mov然后相应地播放它,但是AVPlayer并没有接受它。
您正在使用AVCaptureMovieFileOutput保存视频及其文档状态:
对QuickTime电影的内容进行了一些研究,发现它的标准文件扩展名为.mov:
如果您将文件另存为.mov,则不应再出现问题。