Problem calling ffmpeg.c twice, causing the application to crash?

I am trying to call ffmpeg.c to trim a video based on this video trimmer code. Therefore, when I try to start an activity (which loads and uses its own lib), when I press trin, it works, and I can trim the video, but when I try to start it again, it crashes (and it only works with restarting the application).

So, I spend three days finding a solution to this problem, most of the answers talk about the problem with static variables in ffmpeg.c and creating a lib that loads and unloads the class, fixes the problem ( answer1 , answer2 ). So I tried to apply the solution based on the answers and this github repo in the video trimmer project, but all my attempts failed.

Is there any information about the fork of the 'video-trimmer' project that fixes the problem ?. or someone can give a phased answer on how to implement the solution in the 'video-trimmer' project (because I tried to follow all the solutions on the Internet and apply them in this project, but no luck).

+7
source share
4 answers

the problem seems to be related to initialized values โ€‹โ€‹(some variables are declared global static vars, apparently to facilitate access, but violate the principles of OOP and cause us the problems you encounter), however there are several ways to round this, o what can i think:

  • write a quick function to manually set the static vars to the correct initialization values โ€‹โ€‹(quick and dirty, but it works). The list of methods that should not be allowed to run as they are used later:
    • avcodec_register_all(), avdevice_register_all(), av_register_all()
    • avcodec_find_encoder(), avcodec_find_decoder(), av_find_stream_info()
    • avcodec_open(), avcodec_close()
      • they can be wrapped in a boolean controlled manner, for example, so that if they were executed earlier, they cannot be started again.
  • Another way to manage things is to force the change of variable values โ€‹โ€‹(using a class or structure for managing global vars ffmpeg) that are reinitialized on subsequent launches, for example, when you start a method that currently calls the code to fail, the first step may be manual set the variables back to their default settings so that they work correctly, because at the moment I suspect that you have data left between iterations, and this causes ROBLEM.
  • you can use mutexes to ensure that the above methods behave more responsibly when used with threads.

Addendum:

  • also (at C level) use libffmpeginvoke instead of libffmpeg if you are going to call main () several times
  • force garbage collection (yes, this is another ugly fix) when invoked to load ffmpeg lib, which will then clear everything, allowing you to call another instance

Let me know if you need something deeper: I can try to create a test environment to replicate your problems and see where I get it, although for this I need access to my home PC, since when I am at work I donโ€™t have Android SDK

+2
source

Help us help, please provide the implemented code or part of it. A crash log will also be useful.

Hint: initialize the ffmpeg / thread object. Then use the callback interface. Once VideoTrimmer has finished, call back. In this callback, destruction / destruction of the ffmpeg object / stream is called.

Perhaps this link may help you.

I recently used the project โ€œandroid-ffmpeg-javaโ€ from github, this is a working library, I can guarantee. You just need to implement a shell (test application) that will do the job.
Check this link for source: android-ffmpeg-java
Check this link, for example: android-ffmpeg-cmdline . See if you can solve this problem.

+1
source

I'm not sure if this will help, but C files usually have a header in which you can use

ifndef

Please note the following: http://www.cprogramming.com/reference/preprocessor/ifndef.html

Use this syntax for the sandwhich declaration in the associated .h file to ensure that multiple imports do not cause a failure in the import code.

Good luck

Edit: Well, it looks like this means recompiling ffmpeg in a .so file. You should simply try to verify that it has the mechanism described above in the code base, and try to confirm that it does not load twice.

0
source

While somewhat rude, a potential workaround might be to use / link to ffmpeg from the Service (you should still do this), which is declared in the manifest to run in its own process, and not in the clientโ€™s actions. Then complete this process itself - if necessary, call native exit () when the task is fully completed. Android doesn't particularly like this happening - this is not good practice, but you can probably make it work.

Reconstructing the library in order to be able to reset directly in the new state (or even make it completely contextual) would be better, but for a huge old code base, a large project may turn out to be.

0
source

All Articles