Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:02:49+00:00 2026-06-16T04:02:49+00:00

I try to read a video frame-by-frame from iPhone photo album. After image processing,

  • 0

I try to read a video frame-by-frame from iPhone photo album.
After image processing, I will save them as a new video.
I’m running my code without any error, but there is no new video in the album.

Here is my code.

// Video writer init
- (BOOL)setupAssetWriterForURL:(CMFormatDescriptionRef)formatDescription
{
    float bitsPerPixel;
    CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
    int numPixels = dimensions.width * dimensions.height;
    int bitsPerSecond;

    if ( numPixels < (640 * 480) )
        bitsPerPixel = 4.05; 
    else
        bitsPerPixel = 11.4; 

    bitsPerSecond = numPixels * bitsPerPixel;

    NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                              AVVideoCodecH264, AVVideoCodecKey,
                                              [NSNumber numberWithInteger:dimensions.width], AVVideoWidthKey,
                                              [NSNumber numberWithInteger:dimensions.height], AVVideoHeightKey,
                                              [NSDictionary dictionaryWithObjectsAndKeys:
                                               [NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
                                               [NSNumber numberWithInteger:30], AVVideoMaxKeyFrameIntervalKey,
                                               nil], AVVideoCompressionPropertiesKey,
                                              nil];
    if ([assetWriter canApplyOutputSettings:videoCompressionSettings forMediaType:AVMediaTypeVideo]) {
        assetWriterVideoIn = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings];
        assetWriterVideoIn.expectsMediaDataInRealTime = YES;
        assetWriterVideoIn.transform = [self transformFromCurrentVideoOrientationToOrientation:self.referenceOrientation];
        if ([assetWriter canAddInput:assetWriterVideoIn])
            [assetWriter addInput:assetWriterVideoIn];
        else {
            NSLog(@"Couldn't add asset writer video input.");
            return NO;
        }
    }
    else {
        NSLog(@"Couldn't apply video output settings.");
        return NO;
    }

    return YES;
}

Read video

- (void)readMovie:(NSURL *)url
{
    AVURLAsset * asset = [AVURLAsset URLAssetWithURL:url options:nil];
    [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:
     ^{
         dispatch_async(dispatch_get_main_queue(),
                        ^{
                            AVAssetTrack * videoTrack = nil;
                            NSArray * tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
                            if ([tracks count] == 1)
                            {
                                videoTrack = [tracks objectAtIndex:0];

                                NSError * error = nil;

                                // _movieReader is a member variable
                                AVAssetReader *movieReader = [[AVAssetReader alloc] initWithAsset:asset error:&error];
                                if (error)
                                    NSLog(@"_movieReader fail!\n");

                                NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
                                NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
                                NSDictionary* videoSettings = 
                                [NSDictionary dictionaryWithObject:value forKey:key]; 

                                [movieReader addOutput:[AVAssetReaderTrackOutput 
                                                         assetReaderTrackOutputWithTrack:videoTrack 
                                                         outputSettings:videoSettings]];
                                [movieReader startReading];

                                while ([movieReader status] == AVAssetReaderStatusReading)
                                {
                                    AVAssetReaderTrackOutput * output = [movieReader.outputs objectAtIndex:0];
                                    CMSampleBufferRef sampleBuffer = [output copyNextSampleBuffer];
                                    if (sampleBuffer)
                                    {
                                        if ( !assetWriter ) {
                                            outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%llu.mov", NSTemporaryDirectory(), mach_absolute_time()]];

                                            NSError *error = nil;
                                            assetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:(NSString *)kUTTypeQuickTimeMovie error:&error];
                                            if (error)
                                                [self showError:error];


                                            if (assetWriter) {
                                                CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
                                                [self setupAssetWriterForURL:formatDescription];
                                            }
                                        }
                                        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);


                                        CVPixelBufferLockBaseAddress(imageBuffer,0);

                                        int bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
                                        int bufferWidth = CVPixelBufferGetWidth(imageBuffer);
                                        int bufferHeight = CVPixelBufferGetHeight(imageBuffer);
                                        unsigned char *pixel = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);


                                        for( int row = 0; row < bufferHeight; row++ ) {
                                            for( int column = 0; column < bufferWidth; column++ ) {

                                                pixel[0] = (pixel[0]+pixel[1]+pixel[2])/3;
                                                pixel[1] = (pixel[0]+pixel[1]+pixel[2])/3;
                                                pixel[2] = (pixel[0]+pixel[1]+pixel[2])/3;

                                                pixel += 4;
                                            }
                                        }


                                        CVPixelBufferUnlockBaseAddress(imageBuffer,0);

                                        if ( assetWriter ) {
                                            [self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeVideo];
                                        }


                                        CFRelease(sampleBuffer);
                                    }

                                }
                                if (assetWriter) {
                                    [assetWriterVideoIn markAsFinished];
                                    assetWriter = nil;
                                    [assetWriter finishWriting];
                                    assetWriterVideoIn = nil;
                                    assetWriter = nil;

                                    [self saveMovieToCameraRoll];
                                }
                                else {
                                    [self showError:[assetWriter error]];
                                }

                            }
                        });
     }];

}
- (void) writeSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(NSString *)mediaType
{
    if ( assetWriter.status == AVAssetWriterStatusUnknown ) {

        if ([assetWriter startWriting]) {
            [assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
        }
        else {
            [self showError:[assetWriter error]];
        }
    }

    if ( assetWriter.status == AVAssetWriterStatusWriting ) {

        if (mediaType == AVMediaTypeVideo) {
            if (assetWriterVideoIn.readyForMoreMediaData) {
                if (![assetWriterVideoIn appendSampleBuffer:sampleBuffer]) {
                    [self showError:[assetWriter error]];
                }
            }
        }

    }
}
- (void)saveMovieToCameraRoll
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:outputURL
                                completionBlock:^(NSURL *assetURL, NSError *error) {
                                    if (error){
                                        [self showError:error];
                                    NSLog(@"save fail");
                                }
                                    else
                                    {
                                        [self removeFile:outputURL];
                                        NSLog(@"!!!");
                                    }
                                    });
                                }];
}
- (void)removeFile:(NSURL *)fileURL
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [fileURL path];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSError *error;
        BOOL success = [fileManager removeItemAtPath:filePath error:&error];
        if (!success)
            [self showError:error];
    }
}

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:02:50+00:00Added an answer on June 16, 2026 at 4:02 am

    And Here is an answer for Creating Movie from images.

    Hope You will get Some Help from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i try to read Data from an Oracle-Database. The problem is that in some
I try to read out all existing calendars. I tried the example from here:
I see that when I try to read the value from a textarea field
I get a warning in MSVC++ when I try to read an integer from
After reading this: Code for download video from Youtube on Java, Android I found
I am new in Android, i want to fetch the video file from web
I am getting a url from server & i want to save video in
I am trying to publish a large video/image file from the local file system
I try to read out all existing calendars. I want have a HashMap with
I try to read a text files with 20x20 data into variable C, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.