Search code examples
iosvideoavfoundationuivideoeditorcontroller

AVURLAsset cannot find video track


I use UIVideoEditorController to trim a video. After editing, in the delegate, I try to remix the video with other media. However, [AVURLAsset trackWithMediaType:] is unable to find video track. The code is as follow:

- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
    NSURL *videoURL = [NSURL URLWithString:videoPath];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
    if(asset == nil) {
      NSLog(@"Cannot create AVURLAsset");
      return;
    } else {
      NSLog(@"Asset: %@", asset);
    }

    if([asset tracksWithMediaType:AVMediaTypeVideo].count == 0) {
      NSLog(@"No Video Track");
      return;
    } else if([asset tracksWithMediaType:AVMediaTypeAudio].count == 0) {
      NSLog(@"No Audio Track");
      return;
    }
    // Follow up codes hidden intentionally
}

The Console log prints:

2014-10-15 13:35:45.913 TestVideoTrim[3278:1191544] Asset: <AVURLAsset: 0x178229e00, URL = /private/var/mobile/Containers/Data/Application/96E7C0FF-92EB-44F7-8E35-B29FD0E58302/tmp/trim.EC3FB214-9907-411D-B3A3-75 ... 41E5.MOV>
2014-10-15 13:35:45.981 TestVideoTrim[3278:1191544] No Video Track

However, when I go to Photo App and play the trimmed video, the video plays well with audio. What could be the reason of unable to find video track?

p.s. the delegate function's NSError returns null, representing there is no error occurred when editing the video.


Solution

  • Replace

    NSURL *videoURL = [NSURL URLWithString:videoPath];
    

    with

    NSURL *videoURL = [NSURL fileURLWithPath:videoPath];