Abhijeet Gupta
Abhijeet Gupta

Reputation: 124

Video Trimming via ffmpeg twice crash

I am trying to trim video using ffmpeg lib. Video Trimming is success first time but it crash second time.

For solving this crash, i search for it and use the dlopen() and dlclose() for dynamic loading for ffmpeg lib.

my code is-

const char* path;
void* handle;
void* handle1;
const char *in, *out;
int close;
__android_log_write(ANDROID_LOG_ERROR, "VideoTrimmer Invoke", "Video Trimmer Invoke");
    in = (*env)->GetStringUTFChars(env, inputFile, 0);
    out = (*env)->GetStringUTFChars(env, outFile, 0);

int *(*Java_com_videotrimmingwithnativesample_VideoTrimmer_trim)(JNIEnv*, jclass ,jstring inputFile, jstring outFile, jint startTime, jint length);
path = (*env)->GetStringUTFChars(env, libffmpegPath, 0);
handle = dlopen(path, RTLD_LAZY);
if(!handle)
{
    __android_log_write(ANDROID_LOG_ERROR, "VideoTrimmer Invoke HAndle false", dlerror());
}
else
{
    __android_log_write(ANDROID_LOG_ERROR, "VideoTrimmer Invoke HAndle True", dlerror());
}

Java_com_videotrimmingwithnativesample_VideoTrimmer_trim = dlsym(handle, "Java_com_example_videotrimmingwithnativesample_VideoTrimmer_trim");
if(!Java_com_videotrimmingwithnativesample_VideoTrimmer_trim)
{
    __android_log_write(ANDROID_LOG_ERROR, "VideoTrimmer Invoke dlsym false",dlerror());
}
else
{
    __android_log_write(ANDROID_LOG_ERROR, "VideoTrimmer Invoke dlsym true","Video TrimmerInvoke dlsym true");
}

int i=(*Java_com_videotrimmingwithnativesample_VideoTrimmer_trim)(env, obj, inputFile,outFile,startTime,length);
if(dlclose(handle)==0)
{
    (*env)->ReleaseStringUTFChars(env, libffmpegPath, path);
__android_log_write(ANDROID_LOG_ERROR, "VideoTrimmer Invoke close true","Video TrimmerInvoke close true");
}

Error on second time use show on dlopen line --

Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), (IntentService[V) 

Please help me for removing this crash.

Thanks in advance.

Upvotes: 1

Views: 1391

Answers (4)

HelloCsl
HelloCsl

Reputation: 221

I am using FFmpeg 3.6.6 to build my application and with the same problem. According to my debugging experience ,it happen because the variable "nb_filtergraphs" is not the initial value any more but variable "filtergraphs" is, when this method is executed it will crash the system

static int init_complex_filters(void){
int i, ret = 0;

for (i = 0; i < nb_filtergraphs; i++) {
    ret = init_complex_filtergraph(filtergraphs[i] //crash here);
    if (ret < 0)
        return ret;
    }
return 0;}

the origin call chain is ffmpeg#main(you might change this function name) -> ffmpeg_opt#ffmpeg_parse_options -> ffmpeg_opt#init_complex_filters

My solution is to call the following code after the main method is executed

ffmpeg.c

int main(int argc, char **argv)//you might change the name {
//.....
ffmpeg_cleanup(received_nb_signals ? 255 : main_return_code);
nb_filtergraphs = 0;
progress_avio = NULL;

input_streams = NULL;
nb_input_streams = 0;
input_files = NULL;
nb_input_files = 0;

output_streams = NULL;
nb_output_streams = 0;
output_files = NULL;
nb_output_files = 0;
exit_program(received_nb_signals ? 255 : main_return_code);
return main_return_code;
}

Upvotes: 2

Marc Plano-Lesay
Marc Plano-Lesay

Reputation: 6958

The issue is that ffmpeg uses static variables which are not reinitialized when the first run is complete. On the second run, they still have their values, so ffmpeg fails to initialize itself, and crashes.

You have two possibilites: either unload/reload your library between each run, or modify ffmpeg source code to reset these variables.

Upvotes: 0

Jagdeep Singh
Jagdeep Singh

Reputation: 1210

Just make a method in your ffmpeg.c which will seems like this

void exitmycode(){
       ffmpeg_exit(0);

}

ffmpeg_exit(0) method is already there in the ffmpeg.c you just have to call exitmycode(); from your main C file after the completion of video trimming.

Now what was happening is that when you trim a video or anything else with the ffmpeg it doesn't get exit completely, so the next time you run the command it get exited, but it also don't run your trim command.Again if you run that third time, command get executed perfectly. So, what I had done is calling the ffmpeg_exit(0) manually at the end of processing done.

Upvotes: 0

Robert Rowntree
Robert Rowntree

Reputation: 6289

Your problem may be related to discussion here

The linked thread is old but symptom there was that android/JNI call to ffmpeg worked first time but not the second time.

As noted in the thread, the solution is to explicitly unload/load the library in between successive calls to ffmpeg.

you could try it.

Upvotes: 0

Related Questions