Note that there are some explanatory texts on larger screens.

plurals
  1. POMac OS command line tool handle asynchronous export video for re-encoding (Edited and working now)
    primarykey
    data
    text
    <p>I tested the export method on an iOS application and it works fine. But when I moved it to a command line project the method exportAsynchronouslyWithCompletionHandler doesn't work.</p> <p>I know the possible cause is the command line tool cannot handle asynchronous because it returns immediately. The method is pretty straightforward. It just take the address of the video, specify the quality, and output video to the output path. And here is my code. </p> <pre><code>void exportVideo(NSString *source, NSString *quality, NSString *filePath) { NSString *preset = nil; if ([quality isEqualToString:@"high"]) { preset = AVAssetExportPreset960x540; // 16:9 recommended resolution for iPhone 4 } else if ([quality isEqualToString:@"middle"]) { preset = AVAssetExportPresetAppleM4VWiFi; // 16:9 recommended resolution for iPhone 3GS } // Creat the asset from path AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:source] options:nil]; if (!asset) NSLog(@"There is no video in the asset"); //NSLog(@"Print all presets: %@", [AVAssetExportSession allExportPresets]); //NSLog(@"Print all presets compatible with the asset: %@", [AVAssetExportSession exportPresetsCompatibleWithAsset:asset]); NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset]; //NSLog(@"All available presets: %@", compatiblePresets); if ([compatiblePresets containsObject:preset]) { // create export session AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:preset]; exportSession.outputURL = [NSURL fileURLWithPath:filePath]; // output path if (preset == AVAssetExportPreset960x540) { exportSession.outputFileType = AVFileTypeQuickTimeMovie; } else if (preset == AVAssetExportPresetAppleM4VWiFi) { exportSession.outputFileType = AVFileTypeAppleM4V; } dispatch_semaphore_t sema = dispatch_semaphore_create(0); // Add this line // In command line tool, it doesn't execute this method [exportSession exportAsynchronouslyWithCompletionHandler:^{ if (exportSession.status == AVAssetExportSessionStatusCompleted) { NSLog(@"AVAssetExportSessionStatusCompleted"); } else if (exportSession.status == AVAssetExportSessionStatusFailed) { NSLog(@"AVAssetExportSessionStatusFailed"); NSLog (@"FAIL %@", exportSession.error); } else { NSLog(@"Export Session Status: %ld", exportSession.status); } dispatch_semaphore_signal(sema); // Add this line }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); // Add this line dispatch_release(sema); // Add this line } else { NSLog(@"Requested quality is not available."); } </code></pre> <p>}</p> <pre><code>int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"Input the source file path:"); char str[100]; scanf("%s", str); NSString *inputSource = [NSString stringWithUTF8String:str]; NSLog(@"Print the input source: %@", inputSource); NSLog(@"Input the output video quality:"); scanf("%s", str); NSString *quality = [NSString stringWithUTF8String:str]; NSLog(@"Print the quality: %@", quality); NSLog(@"Input the output file path:"); scanf("%s", str); NSString *outputPath = [NSString stringWithUTF8String:str]; NSLog(@"Print the output path: %@", outputPath); exportVideo(inputSource, quality, outputPath); } return 0; } </code></pre> <p>**The usage of dispatch_semaphore_t works for my case. I got this idea from an answer from stackoverflow. Thanks for all you guys help so I share all the code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload