相关疑难解决方法(0)

如何减少使用UIImagePickerController创建的视频的文件大小?

我有一个应用程序,允许用户录制视频,UIImagePickerController然后将其上传到YouTube.问题是UIImagePickerController创建的视频文件是巨大的,即使视频只有5秒长.例如,5秒长的视频是16-20兆字节.我想保持540或720质量的视频,但我想减少文件大小.

我一直在尝试AVFoundation并AVAssetExportSession试图获得更小的文件大小.我试过以下代码:

AVAsset *video = [AVAsset assetWithURL:videoURL];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetPassthrough];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [pathToSavedVideosDirectory URLByAppendingPathComponent:@"vid1.mp4"];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    NSLog(@"done processing video!");
}];
Run Code Online (Sandbox Code Playgroud)

但这并没有减少文件大小.我知道我正在做的事情是可能的,因为在Apple的照片应用程序中,当您选择"在YouTube上分享"时,会自动处理视频文件,因此它的小到可以上传.我想在我的应用程序中做同样的事情.

我怎么能做到这一点?

video file-upload objective-c avfoundation ios

56
推荐指数
7
解决办法
6万
查看次数

使用AVAsset制作带有图片阵列和歌曲文件的电影文件

我正在尝试使用图片阵列和音频文件制作电影文件.为了制作带有图片阵列的电影,我在这里使用了zoul的大帖子.一切都很完美,我的电影和我的照片一样.但是,当我尝试添加一些音轨时,我遇到了很多问题.要理解我把我的代码:

当我调用此方法时,图片数组和歌曲文件已准备就绪:

-(void) writeImagesToMovieAtPath:(NSString *) path withSize:(CGSize) size
{
    NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:documentsDirectoryPath];
    for (NSString *tString in dirContents) {
        if ([tString isEqualToString:@"essai.mp4"]) 
        {
            [[NSFileManager defaultManager]removeItemAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,tString] error:nil];

        }
    }

    NSLog(@"Write Started");

    NSError *error = nil;

    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                  [NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4
                                                              error:&error];    
    NSParameterAssert(videoWriter);

    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                   AVVideoCodecH264, AVVideoCodecKey,
                                   [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                                   [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                                   nil];

    AudioChannelLayout channelLayout;
    memset(&channelLayout, 0, sizeof(AudioChannelLayout));
    channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;

    NSDictionary …
Run Code Online (Sandbox Code Playgroud)

iphone audio video ios avassetwriter

46
推荐指数
1
解决办法
3万
查看次数

AVFoundation - 反转AVAsset并输出视频文件

我已经看过几次这个问题了,但是他们似乎没有任何合适的答案.

要求是反转并输出视频文件(不仅仅是反向播放),保持与源视频相同的压缩,格式和帧速率.

理想情况下,解决方案能够在内存或缓冲区中完成所有操作,并避免将帧生成到图像文件中(例如:使用AVAssetImageGenerator),然后重新编译它(资源密集,不可靠的计时结果,帧/图像质量从原始,等等.).

-

我的贡献:这仍然没有用,但到目前为止我尝试过的最好:

  • 将示例帧读入一个CMSampleBufferRef[]使用的数组AVAssetReader.
  • 使用相反的顺序将其写回AVAssetWriter.
  • 问题:似乎每个帧的时间都保存在CMSampleBufferRef所以即使向后追加它们也行不通.
  • 接下来,我尝试用反向/镜像帧交换每帧的定时信息.
  • 问题:这会导致未知错误AVAssetWriter.
  • 下一步:我要调查一下 AVAssetWriterInputPixelBufferAdaptor

    - (AVAsset *)assetByReversingAsset:(AVAsset *)asset {
        NSURL *tmpFileURL = [NSURL URLWithString:@"/tmp/test.mp4"];    
        NSError *error;
    
        // initialize the AVAssetReader that will read the input asset track
        AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:&error];
        AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject];
    
        AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:nil];
        [reader addOutput:readerOutput];
        [reader startReading];
    
        // Read in the samples into an array
        NSMutableArray *samples …
    Run Code Online (Sandbox Code Playgroud)

macos objective-c avfoundation ios

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

由于无法识别电影的文件格式,无法打开视频文件

我试图从图像magick库给出的图像创建一个视频文件.像不透明度差异一样逐个应用一些效果后,它会成功创建,但会Quick time player给出错误" video file could not be opened. The movie's file format isn't recognized ".

我使用以下代码:

double d = 0.00;

- (void)posterizeImageWithCompression:(id)sender {

    // Here we use JPEG compression.
    NSLog(@"we're using JPEG compression");

    MagickWandGenesis();
    magick_wand = NewMagickWand();
    magick_wand = [self magiWandWithImage:[UIImage imageNamed:@"iphone.png"]];

    MagickBooleanType status;

    status = MagickSetImageOpacity(magick_wand, d);

    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }
    size_t my_size;
    unsigned char * my_image = MagickGetImageBlob(magick_wand, &my_size); …
Run Code Online (Sandbox Code Playgroud)

iphone imagemagick objective-c ios

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

录制视频上的iPhone水印.

在我的应用程序中,我需要捕获视频并在该视频上添加水印.水印应为文本(时间和注释).我看到使用"QTKit"Frame工作的代码.但是我读到该框架不适用于iPhone.

提前致谢.

iphone watermark video-watermarking

16
推荐指数
3
解决办法
2万
查看次数

将UIImage转换为CVImageBufferRef

这段代码大部分都有效,但结果数据似乎松散了一个颜色通道(正如我所想),因为显示的结果图像数据是蓝色的!

这是代码:

UIImage* myImage=[UIImage imageNamed:@"sample1.png"];
CGImageRef imageRef=[myImage CGImage];
CVImageBufferRef pixelBuffer = [self pixelBufferFromCGImage:imageRef];
Run Code Online (Sandbox Code Playgroud)

方法pixelBufferFromCGIImage是从stackoverflow上的另一个帖子中获取的:如何将UIImage数组导出为电影?(虽然这个应用程序与我想要做的事情无关)但确实如此

+ (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image
{
    CGSize frameSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
    NSDictionary *options = @{
                              (__bridge NSString *)kCVPixelBufferCGImageCompatibilityKey: @(NO),
                              (__bridge NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey: @(NO)
                              };
    CVPixelBufferRef pixelBuffer;
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,
                                          frameSize.height,  kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
                                          &pixelBuffer);
    if (status != kCVReturnSuccess) {
        return NULL;
    }

    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    void *data = CVPixelBufferGetBaseAddress(pixelBuffer);
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(data, frameSize.width, frameSize.height,
                                                 8, CVPixelBufferGetBytesPerRow(pixelBuffer), rgbColorSpace,
                                                 (CGBitmapInfo) kCGImageAlphaNoneSkipLast);
    CGContextDrawImage(context, …
Run Code Online (Sandbox Code Playgroud)

iphone bitmap uiimage ios

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

如何将*单张图像*与视频合并

我正在尝试将单个视频与单个图像组合在一起.这不是试图将许多图像组合成单个视频,例如

AVMutableComposition用来组合曲目.我的应用程序能够组合视频和图像(但就目前而言,视频组合很好!)我尝试使用AVAssetWriter将单个图像转换为视频(我相信这是我的问题,但不是100%肯定) .然后我将其保存到app(documents directory).从那里,我在合并中访问它,并将视频和现在变成视频的图像组合在一起.

流程:

用户选择图像 - >

将图像转换为AVAssetWriter以更改为视频 - >

合并我已预设的视频视频 - >

结果:从所选图像和预设视频制作1个视频.

我的问题:我的代码提供了一个空白区域,视频内部的图像应该是.就像在我的ImageConverter文件中一样,它会将它转换为视频,但我只会看到最后一帧作为图像,而每隔一帧都是透明的,好像图片不存在一样.因此,如果我将图像转换为视频5秒钟(假设是30帧/秒),那么我将看到(30*5)-1帧的空白区域,然后是最后一帧,最终会出现图片.我正在寻找有关如何将单个图像制作成视频将视频和图像组合在一起而不将图像转换为视频的指导.谢谢!

合并文件在这里

func merge() {
    if let firstAsset = controller.firstAsset, secondAsset = self.asset {

        // 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
        let mixComposition = AVMutableComposition()

        let firstTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo,
                                                                     preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
        do {
            try firstTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, CMTime(seconds: 8, preferredTimescale: 600)),
                                           ofTrack: firstAsset.tracksWithMediaType(AVMediaTypeVideo)[0] ,
                                           atTime: kCMTimeZero)
        } …
Run Code Online (Sandbox Code Playgroud)

buffer uiimage avassetwriter avmutablecomposition swift

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

如何将CGImage转换为CMSampleBufferRef?

我想将a转换CGImageCMSampleBufferRefAVAssetWriterInput使用该appendSampleBuffer:方法将其附加到a .我已经设法CMSampleBufferRef使用以下代码,但appendSampleBuffer:只是NO在我提供结果时返回CMSampleBufferRef.我究竟做错了什么?

- (void) appendCGImage: (CGImageRef) frame
{
    const int width = CGImageGetWidth(frame);
    const int height = CGImageGetHeight(frame);

    // Create a dummy pixel buffer to try the encoding
    // on something simple.
    CVPixelBufferRef pixelBuffer = NULL;
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height,
        kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);
    NSParameterAssert(status == kCVReturnSuccess && pixelBuffer != NULL);

    // Sample timing info.
    CMTime frameTime = CMTimeMake(1, 30);
    CMTime currentTime = …
Run Code Online (Sandbox Code Playgroud)

avfoundation core-video core-media ios

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

为什么我的UIImages电影会被扭曲?

我正在使用zoul的解决方案将UIImage数组导出为电影.但我的镜架都变形了.这是原始图片:

在此输入图像描述

这是一个类似的失真例子:http: //oi56.tinypic.com/8wkqys.jpg

我在这里读到它与纵横比有关,但没有解释如何修复它.

iphone export objective-c avfoundation uiimage

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

如何从iPhone框架创建视频

我已经完成了研发工作并取得了成功,如何根据播放的视频文件中的图像获取帧MPMoviePlayerController.

从此代码中获取所有帧,并将所有图像保存在一个阵列中.

for(int i= 1; i <= moviePlayerController.duration; i++)
{
    UIImage *img = [moviePlayerController thumbnailImageAtTime:i timeOption:MPMovieTimeOptionNearestKeyFrame];
    [arrImages addObject:img];
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是,在更改了一些图像文件后,比如为图像添加情感并添加过滤器,例如; 电影真实,黑白,我们如何再次创建视频并以相同的帧速率将相同的视频存储在文档目录中,而不会丢失视频质量.

更改了一些图像后,我按照代码再次保存了该视频.

- (void) writeImagesAsMovie:(NSString*)path 
{
    NSError *error  = nil;
    UIImage *first = [arrImages objectAtIndex:0];
    CGSize frameSize = first.size;
    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                  [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
                                                              error:&error];
    NSParameterAssert(videoWriter);

    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                   AVVideoCodecH264, AVVideoCodecKey,
                                   [NSNumber numberWithInt:640], AVVideoWidthKey,
                                   [NSNumber numberWithInt:480], AVVideoHeightKey,
                                   nil];
    AVAssetWriterInput* writerInput = [[AVAssetWriterInput
                                        assetWriterInputWithMediaType:AVMediaTypeVideo
                                        outputSettings:videoSettings] retain];

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

    NSParameterAssert(writerInput); …
Run Code Online (Sandbox Code Playgroud)

video objective-c mpmovieplayercontroller avfoundation

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