Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to create an AVAsset from the exportSession.outputUrl. #407

Open
abhipermukha opened this issue Sep 29, 2017 · 2 comments
Open

Comments

@abhipermukha
Copy link

I am trying to export the recorded video and then create an AVAsset from the exportSession.outputUrl so that i can use this asset for mixing other audios to the video as per the requirement of my app.
But i am not able to create an AVAsset from the exportSession.outputUrl .

If I save it to camera roll , then i am able to create an AVAsset. But without saving to camera roll, just after the completion of exporting, if i try to create an AVAsset from the exportSession.outputUrl, then it says the duration of the asset is zero seconds(when i print the duration of asset).

Please help me out.

Thanks.

code is as below,

  • (void)saveToCameraRoll {
    self.navigationItem.rightBarButtonItem.enabled = NO;
    SCFilter *currentFilter = [self.filterSwitcherView.selectedFilter copy];
    [_player pause];

    SCAssetExportSession *exportSession = [[SCAssetExportSession alloc] initWithAsset:self.recordSession.assetRepresentingSegments];
    exportSession.videoConfiguration.filter = currentFilter;
    exportSession.videoConfiguration.preset = SCPresetHighestQuality;
    exportSession.audioConfiguration.preset = SCPresetHighestQuality;
    exportSession.videoConfiguration.maxFrameRate = 35;

    exportSession.outputUrl = self.recordSession.outputUrl;

    exportSession.outputFileType = AVFileTypeQuickTimeMovie;//AVFileTypeMPEG4;
    exportSession.delegate = self;
    exportSession.contextType = SCContextTypeAuto;
    self.exportSession = exportSession;

    self.exportView.hidden = NO;
    self.exportView.alpha = 0;
    CGRect frame = self.progressView.frame;
    frame.size.width = 0;
    self.progressView.frame = frame;

    [UIView animateWithDuration:0.3 animations:^{
    self.exportView.alpha = 1;
    }];

    //SCWatermarkOverlayView *overlay = [SCWatermarkOverlayView new];
    //overlay.date = self.recordSession.date;
    //exportSession.videoConfiguration.overlay = overlay;
    //exportSession.videoConfiguration.watermarkFrame = CGRectMake([UIScreen mainScreen].bounds.size.width-210, [UIScreen mainScreen].bounds.size.height-210, 200, 200);

    //exportSession.videoConfiguration.watermarkFrame = CGRectMake(0, 0, 200, 200);
    //exportSession.videoConfiguration.watermarkImage = [UIImage imageNamed:@"watermark"];
    NSLog(@"Starting exporting");

    CFTimeInterval time = CACurrentMediaTime();
    __weak typeof(self) wSelf = self;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
    __strong typeof(self) strongSelf = wSelf;

      if (!exportSession.cancelled) {
          NSLog(@"Completed compression in %fs", CACurrentMediaTime() - time);
      }
    
      if (strongSelf != nil) {
          [strongSelf.player play];
          strongSelf.exportSession = nil;
          strongSelf.navigationItem.rightBarButtonItem.enabled = YES;
    
          [UIView animateWithDuration:0.3 animations:^{
              strongSelf.exportView.alpha = 0;
          }];
      }
    
      NSError *error = exportSession.error;
      if (exportSession.cancelled) {
          NSLog(@"Export was cancelled");
      } else if (error == nil) {
          [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
          [exportSession.outputUrl saveToCameraRollWithCompletion:^(NSString * _Nullable path, NSError * _Nullable error) {
              [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    
              if (error == nil) {
                  [[[UIAlertView alloc] initWithTitle:@"Saved to camera roll" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                  NSLog(@"path = %@",path);
    
                  NSLog(@"exportSession.outputUrl = %@",[exportSession.outputUrl absoluteString]);
                  NSData *videoFile = [NSData dataWithContentsOfURL:exportSession.outputUrl];
                  NSLog(@"videoFile size is : %.2f MB",(float)videoFile.length/1024.0f/1024.0f);
                  
                  AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:[exportSession.outputUrl absoluteString]]];
                  float duration = asset.duration.value/asset.duration.timescale;
    
                  NSLog(@"duration = %f",duration);  //this gets printed as 0.00 , why????????
    
              } else {
                  [[[UIAlertView alloc] initWithTitle:@"Failed to save" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
              }
          }];
      } else {
          if (!exportSession.cancelled) {
              [[[UIAlertView alloc] initWithTitle:@"Failed to save" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
          }
      }
    

    }];
    }

@JayBone16
Copy link

Were you able to figure it out?

@abhipermukha
Copy link
Author

abhipermukha commented Jul 11, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants