Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8204247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:50:17+00:00 2026-06-07T07:50:17+00:00

I tested the export method on an iOS application and it works fine. But

  • 0

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.

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.

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.");
}

}

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;
}

**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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:50:19+00:00Added an answer on June 7, 2026 at 7:50 am

    That is correct, you need to block your main function from returning until the asynch method is complete. The simplest way to do it is with wait flags. Either have a static boolean, or pass a boolean pointer to your export video function. Then while the boolean is false, sleep your main function. This is a terrible practice though, and is only relevant to this small situation since main only does this one thing. Don’t do it in general.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tested out the script below in jsfiddle and it works fine, can someone
I tested each statement on its own and it works, but when I use
Just tested something that works perfect on firefox and chrome doesn't makes anyhing with
I tested: rm \-\-remove-files but I am unable to remove it. How can I
i tested HttpResponse#flushBuffer and PrintWriter#flush on Tomcat 7 below, but it seemed that the
I have tested out hundreds of different codes, either they work and screw something
I developed an app and tested in iPhone and iPad simulators. In the project
I'm having problems testing a flatiron cli app with Mocha . The command-line command
I have developed an Android application and had it tested on emulator as well
I have made a jar file which i tested in windows and it works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.