GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Exported Video has no sound.

Open AJBeltran3 opened this issue 8 years ago • 3 comments

I am using filters to filter a local video on the iOS device. When i export that video and save it to the phones camera roll it no longer has any sound.

-(void) filter: (NSURL *)videoUrl andFilePath : (NSString *) filepath{

GPUImageSepiaFilter *sepia = [[GPUImageSepiaFilter alloc] init];
GPUImageColorInvertFilter *invert = [[GPUImageColorInvertFilter alloc] init];
GPUImageGrayscaleFilter *grayScale = [[GPUImageGrayscaleFilter alloc] init];
GPUImageHazeFilter *haze = [[GPUImageHazeFilter alloc] init];
GPUImageMonochromeFilter *monochrome = [[GPUImageMonochromeFilter alloc] init];

if ([self.story.filterName isEqualToString:@"Sepia"]) {
    videoFilter = sepia;
}
else if ([self.story.filterName isEqualToString:@"Invert"]) {
    videoFilter = invert;
}
else if ([self.story.filterName isEqualToString:@"Gray Scale"]) {
    videoFilter = grayScale;
}
else if ([self.story.filterName isEqualToString:@"Haze"]) {
    videoFilter = haze;
}
else if ([self.story.filterName isEqualToString:@"Monochrome"]) {
    videoFilter = monochrome;
}

_movieFile = [[GPUImageMovie alloc] initWithURL:videoUrl];

[_movieFile addTarget:videoFilter];

NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(1920, 1080)];
[videoFilter addTarget:movieWriter];

movieWriter.shouldPassthroughAudio = YES;
_movieFile.audioEncodingTarget = movieWriter;
[_movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

[movieWriter startRecording];
[_movieFile startProcessing];

 NSString *fileName = [NSString stringWithFormat:@"toloVideo%-d.mp4",arc4random() % 10000];

__unsafe_unretained typeof(self) weakSelf = self;
[movieWriter setCompletionBlock:^{

    NSLog(@"HELLO");

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:movieURL]) {

        [library writeVideoAtPathToSavedPhotosAlbum:movieURL completionBlock:^(NSURL *assetURL, NSError *error)
         {
             [library assetForURL:assetURL
                      resultBlock:^(ALAsset *asset) {

                          [weakSelf.story addExportedUrlInContext:[AppDelegate sharedDelegate].managedObjectContext withStory:weakSelf.story andURL:[assetURL absoluteString] andFilePath:fileName];

                          dispatch_async(dispatch_get_main_queue(), ^{
                              ALAssetRepresentation *representation = asset.defaultRepresentation;
                              weakSelf->_completedStoryVideoImage.image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                                                                        scale:1.0f
                                                                                  orientation:(UIImageOrientation)representation.orientation];



                          });

                          NSLog(@"ASSET URL %@",assetURL);

                          if (error)
                          {
                              NSLog(@"ERROR %@ ", error);
                          }else{

                              NSFileManager *fileManager = [NSFileManager defaultManager];
                              NSError *error;
                              BOOL success = [fileManager removeItemAtPath:filepath error:&error];
                              if (success) {
                                  NSLog(@"Deleted");
                              }
                              else
                              {
                                  NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
                              }

                              NSLog(@"VIDEO SAVED ");
                          }
                      }
                     failureBlock:^(NSError* error) {
                         NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
                     }];

         }];

        NSLog(@"Video Merge SuccessFully");
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    }

    [weakSelf->videoFilter removeTarget:weakSelf->movieWriter];
    [weakSelf->movieWriter finishRecording];
}];

}

Seems like this should be possible and work. If not then there seems to be a bug.

AJBeltran3 avatar Apr 28 '16 22:04 AJBeltran3

Seems like something a few people have complained about lately. Perhaps something changed in the timing in a recent iOS update. Unfortunately, I'm not much good with the AV Foundation audio side of things, and almost all that code was contributed by other developers. I don't record movies with sound in any of my applications.

BradLarson avatar Apr 29 '16 01:04 BradLarson

Sadly, movies without sound look meaningless @BradLarson

1510207073 avatar Nov 19 '18 08:11 1510207073

@imChay did you found any solution for the same?

DhavalUmraliya avatar Mar 16 '20 06:03 DhavalUmraliya