BradLarson / GPUImage

An open source iOS framework for GPU-based image and video processing
http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework
BSD 3-Clause "New" or "Revised" License
20.24k stars 4.61k forks source link

GPUImageMovie "Error reading from file at URL" after 15 times apply GPUImageTransformFilter #2482

Open insikmania opened 7 years ago

insikmania commented 7 years ago

Ive tried every solution posted here but still cant solve this issue. Any help?

vigneshuvi commented 7 years ago

Could be please let us know, How you create file URL?

insikmania commented 7 years ago

I store the video URL to an array:

NSMutableDictionary fileInfo = [[NSMutableDictionary alloc] initWithDictionary:[self.videoURLArray firstObject]]; NSString originalFilename = fileInfo[@"original_filename"]; NSDictionary lastURL = [_videoURLArray lastObject]; NSURL videoURL = lastURL[@"updated_video"] ?: lastURL[@"original_video"]; NSString *path = [NSString stringWithFormat:@"Documents/%@-%lu.mov", originalFilename, filterAttempts];

here's where I store the edited video:

NSString pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:path]; unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie NSURL movieURL = [NSURL fileURLWithPath:pathToMovie];

See Attached Files Archive.zip

vigneshuvi commented 7 years ago

Try this! Simple solution :)

NSMutableDictionary *fileInfo = [[NSMutableDictionary alloc] initWithDictionary:[self.videoURLArray firstObject]];
NSString *originalFilename = fileInfo[@"original_filename"];
NSDictionary *lastURL = [_videoURLArray lastObject];
NSURL *videoURL = lastURL[@"updated_video"] ?: lastURL[@"original_video"];

// Don't assume path / NSString path = [NSString stringWithFormat:@"Documents/%@-%lu.mov", originalFilename, filterAttempts]; */

// Generate file path

NSURL *pathToMovie =  [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:true];
NSURL *path = [[pathToMovie URLByAppendingPathComponent:@"Documents"]  URLByAppendingPathComponent:[NSString stringWithFormat:@"%@-%lu.mov", originalFilename, filterAttempts]];
NSLog(@"---%@",[path absoluteString]);

Don't convert path to NSString and convert to URL. This bad behaviour. Please maintain NSURL.

- (NSDictionary *)setupMovieFileAndReturnVideoURL {

    NSMutableDictionary *fileInfo = [[NSMutableDictionary alloc] initWithDictionary:[self.videoURLArray firstObject]];
    NSString *originalFilename = fileInfo[@"original_filename"];//[VideoWorker filenameOfOriginalAsset:self.videoURLArray];
    NSDictionary *lastURL = [_videoURLArray lastObject];
    NSURL *videoURL = lastURL[@"updated_video"] ?: lastURL[@"original_video"];
    NSURL *pathToMovie =  [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:true];
    NSURL *path = [[pathToMovie URLByAppendingPathComponent:@"Documents"]  URLByAppendingPathComponent:[NSString stringWithFormat:@"%@-%lu.mov", originalFilename, filterAttempts]];
    NSLog(@"---%@",[path absoluteString]);

    return @{@"videoURL": videoURL, @"path": path};
}

- (void)applyTransformFilter:(TransformToolFilter)filterSelected completion:(void(^)(NSURL *transformedVideo))completion {
    NSDictionary *dictionary = [self setupMovieFileAndReturnVideoURL];
    NSURL *videoURL = dictionary[@"videoURL"];
    NSURL *path = dictionary[@"path"]; // NSURL

    [self processAndApplyAdjustmentsAndTransformationsToVideoWithVideoURL:videoURL path:path isUsedForMusic:NO completion:completion];
}

- (void)processAndApplyAdjustmentsAndTransformationsToVideoWithVideoURL:(NSURL *)videoURL path:(NSURL *)movieURL isUsedForMusic:(BOOL)usedForMusic completion:(void(^)(NSURL *filteredVideo))completion {

    @autoreleasepool {

        AVAsset *anAsset = [AVAsset assetWithURL:videoURL];
        AVAssetTrack *videoTrack = [[anAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

        mw = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:[videoTrack naturalSize]];
       }

}
insikmania commented 7 years ago

@vigneshuvi Thanks for the reply, I've tried this solution but still doesn't solve the issue it freezes at 15 attempts. :(

But I notice this changes maybe you can help me with this: