AVEAssetExportSession Fails for Basic Stream sample

Hi,


I'm trying to manipulate video sample that was already downloaded into my device.


I'm consistently getting an error using the code :

let ast = AVAsset.init(url: asset.urlAsset.url)

print(asset.urlAsset.url.absoluteString )

let presets = AVAssetExportSession.exportPresets(compatibleWith: ast)

print (presets)

AVAssetExportSession.determineCompatibility(ofExportPreset: AVAssetExportPresetHEVC1920x1080, with: ast, outputFileType: AVFileType.init("com.apple.m4v-video"), completionHandler: { (isCompatible) in

if !isCompatible {

print("Format not compatible.")

return

}})


let exporter = AVAssetExportSession(asset: ast, presetName: AVAssetExportPresetPassthrough)

print(exporter?.supportedFileTypes)

let filename = "filename.mp4"

print(AVAssetExportSession.exportPresets(compatibleWith: ast) )

let documentsDirectory = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).last!

let outputURL = documentsDirectory.appendingPathComponent(filename)

print(outputURL)

exporter?.outputURL = outputURL

exporter?.outputFileType = AVFileType.mp4

exporter?.exportAsynchronously(completionHandler: {

print ("completed with error : ")

print(exporter?.status.rawValue)

print(exporter?.error)

switch exporter!.status {

case .failed:

print("Export failed: \(String(describing: exporter?.error?.localizedDescription))")

case .cancelled:

print("Export canceled.")

case .completed:

print("Successful!")

default:

break

}

})


I'm getting the following error :


2018-10-03 12:26:24.773233+0300 HLSCatalog[2028:582451] [WindowServer] display_timer_callback: unexpected state (now:1820f3fbc2b < expected:1820f458a19)

file:///private/var/mobile/Containers/Data/Application/270D782C-EB82-4050-AB5D-AF794E6B3470/Library/com.apple.UserManagedAssets.uuRgiT/Basic%2520Stream_8CDF279C6AFC98FE.movpkg/

["AVAssetExportPreset1920x1080", "AVAssetExportPresetLowQuality", "AVAssetExportPresetHEVCHighestQuality", "AVAssetExportPreset640x480", "AVAssetExportPreset3840x2160", "AVAssetExportPresetHEVC3840x2160", "AVAssetExportPresetHighestQuality", "AVAssetExportPresetMediumQuality", "AVAssetExportPreset1280x720", "AVAssetExportPreset960x540", "AVAssetExportPresetHEVC1920x1080"]

2018-10-03 12:26:25.293725+0300 HLSCatalog[2028:582498] [] [12:26:25.293] remoteXPCAsset_CopyTracksWithMediaType signalled err=-12174 (kFigAssetError_TrackNotFound) (No tracks with that mediaType) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/EmbeddedCoreMedia/EmbeddedCoreMedia-2282.12/Prototypes/FigAsset/ClientServer/FigAsset_RemoteXPC.c:1643

2018-10-03 12:26:25.293811+0300 HLSCatalog[2028:582498] [] [12:26:25.294] remoteXPCAsset_CopyTracksWithMediaType signalled err=-12174 (kFigAssetError_TrackNotFound) (No tracks with that mediaType) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/EmbeddedCoreMedia/EmbeddedCoreMedia-2282.12/Prototypes/FigAsset/ClientServer/FigAsset_RemoteXPC.c:1643

Optional([__C.AVFileType(_rawValue: com.apple.quicktime-movie), __C.AVFileType(_rawValue: com.apple.m4a-audio), __C.AVFileType(_rawValue: public.mpeg-4), __C.AVFileType(_rawValue: com.apple.m4v-video), __C.AVFileType(_rawValue: public.3gpp), __C.AVFileType(_rawValue: org.3gpp.adaptive-multi-rate-audio), __C.AVFileType(_rawValue: com.microsoft.waveform-audio), __C.AVFileType(_rawValue: public.aiff-audio), __C.AVFileType(_rawValue: public.aifc-audio), __C.AVFileType(_rawValue: com.apple.coreaudio-format)])

["AVAssetExportPreset1920x1080", "AVAssetExportPresetLowQuality", "AVAssetExportPresetHEVCHighestQuality", "AVAssetExportPreset640x480", "AVAssetExportPreset3840x2160", "AVAssetExportPresetHEVC3840x2160", "AVAssetExportPresetHighestQuality", "AVAssetExportPresetMediumQuality", "AVAssetExportPreset1280x720", "AVAssetExportPreset960x540", "AVAssetExportPresetHEVC1920x1080"]

Format not compatible.

file:///var/mobile/Containers/Data/Application/270D782C-EB82-4050-AB5D-AF794E6B3470/Documents/filename.mp4

completed with error :

Optional(4)

Optional(Error Domain=AVFoundationErrorDomain Code=-11822 "Cannot Open" UserInfo={NSLocalizedFailureReason=This media format is not supported., NSLocalizedDescription=Cannot Open, NSUnderlyingError=0x2804d1410 {Error Domain=NSOSStatusErrorDomain Code=-16976 "(null)"}})

Export failed: Optional("Cannot Open")


Any help will be appreciated !

Thanks.

I think you've got your checks wrong. You have:


AVAssetExportSession.determineCompatibility(ofExportPreset: AVAssetExportPresetHEVC1920x1080, with: ast, outputFileType: AVFileType.init("com.apple.m4v-video"), completionHandler: {
      (isCompatible) in
      if !isCompatible {
          print("Format not compatible.")
          return
     }
})

The result of "determineCompatibility(ofExportPreset:" is provided to a completion handler, which is asynchronous. So, the rest of your code gets on with setting up the export session, not realizing that the file is not in an incompatible format. That means your later code fails weirdly, because it relies on an assumption that isn't true.


Note that "Format not compatible." appears in your log after the second time you print the presets. That's when the completion handler was actually called, which is too late.


What you (kinda) need to do is move the rest of the code inside the completion handler, so it runs after the actual compatibility check.


Aside from that, your input is some kind of movie package, and probably isn't something that AVFoundation can open directly. Maybe the movie you want is somewhere inside the package?

I agree with the idea that the code should be inside the async delegate.

However ,I see two API returning two different things.


determindeCompatibility returns fail while supportedFileTypes and exportPresets return all the formats inside the same list (printed in the console)


Anyway, I didnt find in the documentation any information about allowed media types. I'm trying to export an HLS content that was downloaded from the internet - starting with apple's basic stream sample.

You're asking two different things. In the first test, you're forcing the file type to a specific value. In the later tests, you're letting the APIs determine the file type from the actual asset, so that may give different results.


However, this may not be why it's ultimately failing. You've configured the export session with "AVAssetExportPresetPassthrough", but then set the output type to AVFileType.mp4. You probably cannot pass AVFileType.m4v through to AVFileType.mp4 without a conversion.


I suspect that's what the "No tracks with that mediaType" errors are telling you. The final error, "This media format is not supported." may mean that the input has no tracks that can be passed through, rather than that the media format is unknown.

Hi, Did you resolve the error? I'm currently facing the same error.

AVEAssetExportSession Fails for Basic Stream sample
 
 
Q