Search code examples
javascriptiosnativescriptphassetavasset

Convert PHAsset/AVAsset to mp4 video in Nativescript app


I use the nativescript-imagepicker-plugin for a filepicker. This returns a PHAsset. I have to copy it to a temporary directory to upload it. Im new in iOS, so I tried a bit:

const options = PHVideoRequestOptions.new();
options.version = PHVideoRequestOptionsVersion.Current;
PHImageManager
    .defaultManager()
    .requestAVAssetForVideoOptionsResultHandler(
        phAsset
        , options
        , (avAsset, audioMix, info) => {
            try {

                const tempFilePath = path.join(tempFolderPath, `${Date.now()}.mp4`);
                const targetURL = NSURL.fileURLWithPath(tempFilePath);
                const exportSession = AVAssetExportSession.alloc(avAsset, AVAssetExportPresetPassthrough);
                exportSession.outputUrl = targetURL;
                exportSession.outputFileType = AVFileTypeMPEG4;
                exportSession.exportAsynchronouslyWithCompletionHandler(() => {
                    console.log(exportSession.status);
                });
            }
            catch (e) {
                console.log(e);
            }
        }
    );

My code crashes without error, so I don't know where to start to debug. I want a MP4, to show it in web too.

At the end I need a string (path) to a mp4 file to upload id with nativescript-background-http.


Solution

  • Your syntax seems to be wrong

    const exportSession = AVAssetExportSession.alloc().initWithAssetPresetName(avAsset, AVAssetExportPresetPassthrough);
    exportSession.outputURL = targetURL;