由图像组成的AVPlayerItem

时间:2015-02-26 17:03:15

标签: ios objective-c video avplayer avplayeritem

我需要创建一个可变长度的静音“视频”(即它只是一个图像),我可以在ios上的AVPlayer中使用它。

有没有人知道我可以创建一个AVPlayerItem的方法,它只包含一个持续n秒的图像?

如果我必须生成.mov文件,我需要该文件非常小。

2 个答案:

答案 0 :(得分:2)

好的,我已经写了自己的视频。事实证明,如果你在第一个和最后一个关键帧上写下你想要的图像的视频(那些是唯一的关键帧),那么你得到一个不太好的紧凑视频,而且#34;"" 34;很想写。

我的代码如下:

- (CVPixelBufferRef) createPixelBufferOfSize: (CGSize) size fromUIImage: (UIImage*) pImage
{
    NSNumber*           numYes      = [NSNumber numberWithBool: YES];
    NSDictionary*       pOptions    = [NSDictionary dictionaryWithObjectsAndKeys:   numYes, kCVPixelBufferCGImageCompatibilityKey,
                                                                            numYes, kCVPixelBufferCGBitmapContextCompatibilityKey,
                                                                            nil];

    CVPixelBufferRef    retBuffer   = NULL;
    CVReturn            status      = CVPixelBufferCreate( kCFAllocatorDefault, size.width, size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef)pOptions, &retBuffer );

    CVPixelBufferLockBaseAddress( retBuffer, 0 );
    void*               pPixelData  = CVPixelBufferGetBaseAddress( retBuffer );

    CGColorSpaceRef     colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef        context     = CGBitmapContextCreate( pPixelData, size.width, size.height, 8, 4 * size.width, colourSpace, (CGBitmapInfo)kCGImageAlphaNoneSkipFirst );

    CGSize              inSize      = pImage.size;
    float               inAspect    = inSize.width  / inSize.height;
    float               outAspect   = size.width    / size.height;

    CGRect drawRect;
    if ( inAspect > outAspect )
    {
        float   scale   = inSize.width / size.width;
        CGSize  outSize = CGSizeMake( size.width, inSize.height / scale );

        drawRect        = CGRectMake( 0, (size.height / 2) - (outSize.height / 2), outSize.width, outSize.height );
    }
    else
    {
        float   scale   = inSize.height / size.height;
        CGSize  outSize = CGSizeMake( inSize.width / scale, size.height );

        drawRect        = CGRectMake( (size.width / 2) - (outSize.width / 2), 0, outSize.width, outSize.height );
    }

    CGContextDrawImage( context, drawRect, [pImage CGImage] );

    CGColorSpaceRelease( colourSpace );
    CGContextRelease( context );

    CVPixelBufferUnlockBaseAddress( retBuffer, 0 );

    return retBuffer;
}

- (void) writeVideo: (NSURL*) pURL withImage: (UIImage*) pImage ofLength: (NSTimeInterval) length
{
    [[NSFileManager defaultManager] removeItemAtURL: pURL error: nil];

    NSError*                pError              = nil;
    AVAssetWriter*          pAssetWriter        = [AVAssetWriter assetWriterWithURL: pURL fileType: AVFileTypeQuickTimeMovie error: &pError];

    const int               kVidWidth           = 1920;//pImage.size.width;
    const int               kVidHeight          = 1080;//pImage.size.height;

    NSNumber*               numVidWidth         = [NSNumber numberWithInt: kVidWidth];
    NSNumber*               numVidHeight        = [NSNumber numberWithInt: kVidHeight];

    NSDictionary*           pVideoSettings      = [NSDictionary dictionaryWithObjectsAndKeys:   AVVideoCodecH264,   AVVideoCodecKey,
                                                                                                numVidWidth,        AVVideoWidthKey,
                                                                                                numVidHeight,       AVVideoHeightKey,
                                                                                                nil];

    AVAssetWriterInput*     pAssetWriterInput   = [AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeVideo
                                                                                     outputSettings: pVideoSettings];
    [pAssetWriter addInput: pAssetWriterInput];

    AVAssetWriterInputPixelBufferAdaptor*    pAssetWriterInputPixelBufferAdaptor    =
                                                [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput: pAssetWriterInput
                                                                                                                 sourcePixelBufferAttributes: pVideoSettings];

    __block volatile int finished = 0;

    [pAssetWriter   startWriting];
    [pAssetWriter   startSessionAtSourceTime: kCMTimeZero];

    // Write the image.
    CVPixelBufferRef        pixelBuffer     = [self createPixelBufferOfSize: CGSizeMake( kVidWidth, kVidHeight ) fromUIImage: pImage];

    [pAssetWriterInputPixelBufferAdaptor appendPixelBuffer: pixelBuffer withPresentationTime: kCMTimeZero];
    [pAssetWriterInputPixelBufferAdaptor appendPixelBuffer: pixelBuffer withPresentationTime: CMTimeMake( length * 1000000, 1000000 )];

    CVPixelBufferRelease( pixelBuffer );

    [pAssetWriterInput  markAsFinished];

    // Set end time accurate to micro-seconds.
    [pAssetWriter   endSessionAtSourceTime: CMTimeMake( length * 1000000, 1000000 )];
    [pAssetWriter   finishWritingWithCompletionHandler: ^
        {
            OSAtomicIncrement32( &finished );
        }];

    // Wait for the writing to complete.
    while( finished == 0 )
    {
        [NSThread sleepForTimeInterval: 0.01];
    }
}

您可能会注意到我将视频设置为始终为1920x1080并将图像信箱放置到位。

答案 1 :(得分:1)

您可以从该图片创建一个.mov视频,播放时间非常短暂,让我们说一秒钟,并使用

循环播放此视频
yourplayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

[[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[yourplayer currentItem]];


- (void)playerItemDidReachEnd:(NSNotification *)notification {
        [[yourplayer currentItem] seekToTime:kCMTimeZero];
}

如果视频的有效期为n秒,那么您可以在playerItemDidReachEnd方法中使用计数器并设置限制。