标签: avassetwriter

将音频与视频Objective-C合并

我正在尝试使用以下方法将音频文件与视频文件合并:

+ (void)CompileFilesToMakeMovie:(NSString *) audioPath {
NSLog(@"a");
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSURL *audio_inputFileUrl = [[NSURL alloc] initFileURLWithPath:audioPath];



NSString *videoPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"input" ofType:@"mov"];
NSURL*    video_inputFileUrl = [NSURL fileURLWithPath:videoPath];

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:@"OutputFile.mov"];
NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
NSLog(@"b");


CMTime nextClipStartTime = kCMTimeZero;

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ios avassetwriter

2
推荐指数
1
解决办法
3292
查看次数

AVAssetReader / AVAssetWriter 加入不同分辨率的mp4文件

我正在编写一个 iPad 应用程序,我需要在其中加入不同分辨率的 mp4 文件。为此,我使用 AVAssetReader 的组合来读取 mp4 源文件,并使用 AVAssetWriter 将这些源文件写入单个 mp4 输出文件中。

我尝试使用 AVAssetExportSession 但我遇到的问题是不同的连接文件之间存在黑框。

我现在面临的问题是,一切看起来都正常,但 AVAssetWriter 的完成处理程序从未被调用。

这是我的选择器,将 mp4 文件 URL 列表、单个输出文件 URL 和完成处理程序作为输入。

- (void)resizeAndJoinVideosAtURLs:(NSArray *)videoURLs toOutputURL:(NSURL *)outputURL withHandler:(void(^)(NSURL *fileURL))handler
{
    /*
     First step: create the writer and writer input
     */
    NSError *error = nil;
    self.videoAssetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeMPEG4 error:&error];

    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,[NSNumber numberWithInt:640], AVVideoWidthKey,[NSNumber numberWithInt:480], AVVideoHeightKey,nil];

    AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
    videoWriterInput.expectsMediaDataInRealTime = NO;

    if([self.videoAssetWriter canAddInput:videoWriterInput])
    {
        [self.videoAssetWriter addInput:videoWriterInput];
        [self.videoAssetWriter …
Run Code Online (Sandbox Code Playgroud)

mp4 objective-c ios avassetwriter avassetreader

2
推荐指数
1
解决办法
4236
查看次数

当CTCallStateIncoming时,AVAssetWriter完成写入失败

我使用AVAssetWriter AVCaptureSession来录制视频.工作得很好.我使用UIApplicationDidEnterBackgroundNotification和CTCallCenter.callEventHandler在应用程序进入后台或来电时停止记录.UIApplicationDidEnterBackgroundNotification工作正常.但在CTCallCenter.callEventHandler中,[AVAssetWriter finishWriting]返回NO.这是AVAssetWriter.error:

Error Domain = AVFoundationErrorDomain Code = -11800"这项操作无法完成"UserInfo = 0x6c0bc20 {NSLocalizedFailureReason =发生未知错误(-12785),NSUnderlyingError = 0x6c0fc80"操作无法完成.(OSStatus error -12785.)", NSLocalizedDescription =这项操作无法完成}

当来电时,AVAssetWriter似乎立即失败.录制的文件未完成且无法播放.有人告诉我该如何处理?

CTCallCenter代码:

    m_callCenter = [[CTCallCenter alloc] init];
    m_callCenter.callEventHandler= ^(CTCall* call)
    {
        if (call.callState == CTCallStateDialing || call.callState == CTCallStateIncoming){
            [self stopRecording];
            //[self performSelectorOnMainThread:@selector(stopRecording) withObject:nil waitUntilDone:NO];
        }
    };
Run Code Online (Sandbox Code Playgroud)

stopRecording在其他情况下工作正常.

recording avassetwriter

1
推荐指数
1
解决办法
1402
查看次数

使用 AVCaptureVideoDataOutput 录制视频

我不确定我应该在方法中放什么

- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;
Run Code Online (Sandbox Code Playgroud)

为了将帧写入视频。任何人都可以与我分享这种方法的代码正文,结果是将帧记录到电影中吗?

我以为我的 assetWriter 和 videoInput 设置正确,但我得到的只是一部重复使用 1 帧的电影。

avfoundation ios avassetwriter

1
推荐指数
1
解决办法
6932
查看次数

AVAssetWriter减速

我正在使用AVAssetWriter来保存来自摄像头的实时信息.这很适合使用此代码

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer  fromConnection:(AVCaptureConnection *)connection{ 

 CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
 CMTime lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);

 if(videoWriter.status != AVAssetWriterStatusWriting){
    [videoWriter startWriting];
    [videoWriter startSessionAtSourceTime:lastSampleTime];
 }

 if(adaptor.assetWriterInput.readyForMoreMediaData) [adaptor appendPixelBuffer:imageBuffer withPresentationTime:lastSampleTime];
 else NSLog(@"adaptor not ready",);
}
Run Code Online (Sandbox Code Playgroud)

我通常接近30 fps(但是其他人注意到iPhone 4s上的速度不是60 fps),而当定时[适配器appendPixelBuffer]它只需要几毫秒.

但是,我不需要全帧,但我需要高质量(低压缩,每帧关键帧),我将在几次后读回一个过程.因此,我想在写作之前裁剪图像.幸运的是我只需要一个中间的条带,所以我可以做一个简单的缓冲区memcpy.为此,我创建了一个CVPixelBufferRef,我正在使用适配器进行复制和写入:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer  fromConnection:(AVCaptureConnection *)connection{ 

 CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
 CMTime lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);

 if(videoWriter.status != AVAssetWriterStatusWriting){
    [videoWriter startWriting];
    [videoWriter startSessionAtSourceTime:lastSampleTime];
 }

 CVPixelBufferLockBaseAddress(imageBuffer,0);
 size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
 size_t width = CVPixelBufferGetWidth(imageBuffer);
 size_t height = CVPixelBufferGetHeight(imageBuffer);
 void * buffIn = CVPixelBufferGetBaseAddress(imageBuffer);

 CVPixelBufferRef pxbuffer = …
Run Code Online (Sandbox Code Playgroud)

avfoundation ios avassetwriter

1
推荐指数
1
解决办法
2884
查看次数

不推荐使用finishWriting:首先在iOS 6.0中弃用

AVAssetWriter用来编码来自图像集的视频:

[assetWriter finishWriting];
Run Code Online (Sandbox Code Playgroud)

并且此方法在iOS 6中生成警告.

[assetWriter finishWritingWithCompletionHandler:<#^(void)handler#>];
Run Code Online (Sandbox Code Playgroud)

这就是我在iOS 6中应用它的方式.任何人都可以帮我应用它

iphone ios avassetwriter ios6

1
推荐指数
1
解决办法
5934
查看次数

AVFoundation 从错误的帧速率图像创建视频

我正在尝试使用 AVFoundation 从图像创建视频。关于这种方法已经有多个线程,但我相信它们中的许多都与我在这里面临的问题相同。

视频在 iPhone 上可以正常播放,但不能在 VLC 上播放,例如在 Facebook 和 Vimeo 上也不能正常播放(有时某些帧不同步)。VLC 说视频的帧速率是 0.58 fps,但它应该超过 24 对吧?

有谁知道是什么导致了这种行为?

这是用于创建视频的代码:

self.videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:videoOutputPath] fileType:AVFileTypeMPEG4 error:&error];
    // Codec compression settings
    NSDictionary *videoSettings = @{
                                    AVVideoCodecKey : AVVideoCodecH264,
                                    AVVideoWidthKey : @(self.videoSize.width),
                                    AVVideoHeightKey : @(self.videoSize.height),
                                    AVVideoCompressionPropertiesKey : @{
                                            AVVideoAverageBitRateKey : @(20000*1000), // 20 000 kbits/s
                                            AVVideoProfileLevelKey : AVVideoProfileLevelH264High40,
                                            AVVideoMaxKeyFrameIntervalKey : @(1)
                                            }
                                    };

    AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];

    AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                     assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
                                                     sourcePixelBufferAttributes:nil];

    videoWriterInput.expectsMediaDataInRealTime = NO;
    [self.videoWriter addInput:videoWriterInput];
    [self.videoWriter …
Run Code Online (Sandbox Code Playgroud)

frame-rate video-processing avfoundation avassetwriter

1
推荐指数
1
解决办法
2020
查看次数

如何创建一个outputSettings Dictionary&lt;String. Any&gt; 与 AVAssetWriterInput 实例一起使用吗?

我正在尝试将 MPMediaItem 实例转换为 caf 格式的音频文件。我一直在关注 Chris Adamson 的工作和他的帖子《从 iPod 库到 PCM 样本,步骤比以前少得多》

当我深入研究如何在 Swift 中做到这一点时,我发现了 Abel Domingues github FileConverter.swift在 Swift 中做到了这一点。

然后我开始转换为 Swift 3 作为协议的扩展。一切都很顺利,直到我尝试运行它。它在对象创建时崩溃assetWriterInput,似乎与outputSettings变量有关。

        var outputSettings = [
            AVFormatIDKey: kAudioFormatLinearPCM,
            AVSampleRateKey: 44100,
            AVNumberOfChannelsKey: 2,
            AVChannelLayoutKey: NSData(bytes:&channelLayout, length:MemoryLayout<AudioChannelLayout>.size),
            AVLinearPCMBitDepthKey: 16,
            AVLinearPCMIsNonInterleaved: false,
            AVLinearPCMIsFloatKey: false,
            AVLinearPCMIsBigEndianKey: false
        ] as [String : Any]

        // create an asset writer input
        let assetWriterInput = AVAssetWriterInput(mediaType:AVMediaTypeAudio, outputSettings:outputSettings as NSDictionary as! [String : Any])
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息如下:

-[_SwiftValue unsignedIntValue]: unrecognized selector sent …

avfoundation avassetwriter swift3 avassetwriterinput

1
推荐指数
1
解决办法
2340
查看次数

使用 AVAssetWriter 损坏视频捕获音频和视频

我正在使用 anAVCaptureSession来使用视频和音频输入并使用AVAssetWriter.

如果我不写音频,视频会按预期编码。但是,如果我编写音频,则会收到损坏的视频。

如果我检查CMSampleBuffer提供给AVAssetWriter它的音频,它会显示以下信息:

invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = <CMAudioFormatDescription 0x17410ba30 [0x1b3a70bb8]> {
mediaType:'soun' 
mediaSubType:'lpcm' 
mediaSpecific: {
    ASBD: {
        mSampleRate: 44100.000000 
        mFormatID: 'lpcm' 
        mFormatFlags: 0xc 
        mBytesPerPacket: 2 
        mFramesPerPacket: 1 
        mBytesPerFrame: 2 
        mChannelsPerFrame: 1 
        mBitsPerChannel: 16     } 
    cookie: {(null)} 
    ACL: {(null)}
    FormatList Array: {(null)} 
} 
extensions: {(null)}
Run Code Online (Sandbox Code Playgroud)

由于它提供 lpcm 音频,因此我AVAssetWriterInput使用此设置配置了声音(我尝试了一个和两个通道):

var channelLayout = AudioChannelLayout()
memset(&channelLayout, 0, MemoryLayout<AudioChannelLayout>.size);
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono

let audioOutputSettings:[String: …
Run Code Online (Sandbox Code Playgroud)

audio ios avcapturesession avassetwriter swift

1
推荐指数
1
解决办法
1658
查看次数

没有在PhotoLibrary iPad中获得结果电影

我正在创建一个带有一系列图像和一个音频文件的电影,并将创建的电影保存到照片库,在模拟器中我正在使用我的图像和音频文件阵列获得正确的电影但是在设备中运行时我没有得到我的结果电影相反,我得到了我添加的视频(我在我的代码中使用的OutPut.mov)来编写我的输出文件(混合音频和图像文件数组).

这是我的代码将我的混合音频和图像数组文件保存到照片库:

AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSString* audio_inputFilePath = [[NSBundle mainBundle] pathForResource:@"Ruler" ofType:@"caf"];
NSURL*    audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;


NSString *video_inputFilePath = [documentsDirectory stringByAppendingPathComponent:@"videoOutput1234videoOutput1234.mp4"];
NSLog(@"output url %@",video_inputFilePath);


NSURL*    video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];


NSString* outputFilePath = [[NSBundle mainBundle] pathForResource:@"OutPut" ofType:@"mov"];
NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];


if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];

CMTime nextClipStartTime …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c avassetwriter avmutablecomposition

0
推荐指数
1
解决办法
195
查看次数

通过 AVMutableVideoComposition 和 CAAnimation 输出黑色视频

我正在构建能够录制视频并使用 和 在录制的视频上添加动画叠加的 iOS 应用AVFoundation程序CAAnimation。所有子功能都工作正常,但播放动画时视频背景的结尾是黑色的。它没有渲染我选择的背景视频。我曾经AVAssetWriter录制过视频,并且在相机胶卷上播放得很好。但如果我使用这个录制的视频添加叠加,视频末尾的背景是黑色的。有趣的是,如果我通过本机 iOS 相机应用程序录制视频并使用它添加叠加层,它就可以完美工作。我已经检查过这个问题,但对我没有用。黑色视频 CAAnimation 和 AVFoundation AVAssetExportSession

任何帮助将不胜感激。谢谢

avfoundation caanimation ios avassetwriter avmutablecomposition

0
推荐指数
1
解决办法
716
查看次数