B4A Library FFmpeg_b4a - a FFmpeg library for b4a (decoding/streaming)

FFmpeg_b4a

EDIT: As of August 2019, this library is not compliant with Google Play store's latest requirements which require native 64bits libs. It is still useable for apps not being distributed on Google Play Store though.

Personally I would probably use the ExoPlayer (search the forum) these days unless you have codec-issues. Alternatively, you can use my Vitamio5 library/wrapper which is compliant with Google Play store's latest requirements.


FFmpeg_b4a
wraps the IJKPlayer library. Before FFmpeg_b4a, the only FFmpeg library available for B4A was Vitamio (for which I wrote a wrapper library back in 2012 - see here). My library for Vitamio was later enhanced by @warwound to support more recent versions of Vitamio - see here.

What is so special with FFmpeg and these libraries?
-Well, these libraries work like Android's default MediaPlayer and VideoView (see b4a's Audio-library) except that they include much more powerful features. They include a collection of media demuxers, decoders, filters - with FFmpeg you can decode many formats which are not supported natively by Android.

Why this new FFmpeg_b4a wrapper when there is already Vitamio?
- Good question! I wrote this library mainly due to licensing costs. As long as you develop personal and free apps (and with no ads) with Vitamio integrated, you can use Vitamio for free if I have understood things correctly. However, if you publish a pay app, an app with ads or develop an app for a third party where Vitamio is integrated, then you need a license which of course is fair enough. However, when I contacted Vitamio asking for license costs, I was informed the license costs were in thousands USD per app and per year! Well, this may be OK for larger companies but for a hobby programmer like myself (and probably others here on the forum), it is far too expensive. With the FFmpeg_b4a wrapper I believe the license costs are not any longer necessary. Please refer to the "read-me" in the link I cited above for IJKPlayer where the author of IJKPlayer explains licensing issues. I think the important thing here is that the author wrote the C++ code and the JAVA wrapper code himself and that his work may be used also in commercial projects. Please note that I am not a lawyer so I cannot be certain about this so don't blame me if you run into any licensing problems. However, personally I feel more confident to include this library in commercial projects compared to Vitamio.

Any disadvantages with FFMpeg_b4a compared to Vitamio?
-well, Vitamio is very mature and exposes more methods and attributes than FFmpeg_b4a. However, the only things that I personally miss in FFmpeg_b4a are support for subtitles and multiple audio tracks. Some other things missing in FFMmeg_b4a are: possibility to select video-quality (for now I believe all streams are "highest quality" only), no support for MIPS, you cannot set buffer-size and some other minor things. However, most methods and attributes available in Vitamio are available also in FFmpeg_b4a. The implementation of the methods are similar and use mostly the same syntax so replacing Vitamio should be rather easy and straight-forward. Hopefully the author of IJKPlayer will continue improving the library and add missing features.

How to use FFmpeg_b4a?

-Requirements:
a) You need B4A v3.20 or later to compile your app.
b) Min SDK version is API level 9 (Android 2.3–2.3.2 Gingerbread)

-Instructions:
1)In your B4A-project (refer to the included demo in the attached zip-file), under Project attributes, add 2 lines as follows:
B4X:
#AdditionalRes: C:\testingb4a\testffmpeg\ijkmediaplayer\res, tv.danmaku.ijk.media.player
#AdditionalRes: C:\testingb4a\testffmpeg\ijkmediawidget\res, tv.danmaku.ijk.media.widget

IMPORTANT - Replace "C:\testingb4a\testffmpeg\ijkmediaplayer\res" and "C:\testingb4a\testffmpeg\ijkmediawidget\res" with the path to your folder where you saved the supplied res-folders on your PC.
The res-folders are included in the attached zip-file. Please note that the res-folder for ijkmediaplayer is empty. I am unsure if B4A still needs the path when compiling so I included it anyway.

2) Copy the libraries (ijkmediaplayer.jar and ijkmediawidget.jar) included in the attached zip-file to your B4A extra-library folder.

3) The wrapper with the necessary FFmpeg-library (support for armeabi, armeabi-v7a and x86) is available here:
- FFmpeg_B4A - first version
- FFmpeg_B4A_v2 - added support for rtsp and some extra codecs
- FFmpeg_B4A_v3 - added support for some speech codecs suitable for IP-cams (https://www.dropbox.com/s/j5fqvgkl4be4dtk/FFMpeg_B4A_v3.zip?dl=0)

Extract and copy the library (jar-file and xml-file) into your B4A extra-library folder.

4) In B4A (in the the libs-tab), remember to tick the FFmpeg_b4a library.

Notes:
-armeabi, armeabi-v7a and x86 architecture are supported while ARMv5, ARMv6, MIPS are not.
-If the author of the IJKPlayer adds new features and I think it would be a worthwhile upgrade, I will try to include them and release an updated version of the wrapper.

Important final notes:
-Compared to the original FFmpeg build by bbcallen, I built and recompiled the basic FFmpeg-configuration by adding some codecs which I needed. I built the FFmpeg-library according to my needs but I think the FFmpeg_b4a wrapper should now work with most containers and video/audio decoders. This means that the FFmpeg-library is also rather light-weight compared to others.
-If you have problems with some streams or with playback of some video/audio-files, you can check the unfiltered logs in B4A and look for a line similar to the following: "No codec could be found with id XXX".
If you see this, then it is likely you need a codec which is not included the FFmpeg-library. Further down in the unfiltered log, you should see an indication which codec is missing. You can then add the missing containers, demuxers, muxers and codec by building and recompiling the FFmpeg-libraries yourself and then replace the existing FFmpeg-libraries (so-files) in the FFmpeg_B4A wrapper with the ones you generated (you can use 7Zip for that). In this way, you are independent as long as there are no major changes in the wrapper itself which could break the code.
-The wrapping of the library itself was not tricky. The difficult and time-consuming part was actually learning how to set up everything on a Linux-box, configure everything (dependencies, system environments etc.), get some basic understanding of FFmpeg in order to create and/or modify the FFmpeg-libraries, trouble-shooting errors and make sure that everything worked. I spent quite some time on this to get a better understanding. Most projects which can be found on the Internet are not supplying the FFmpeg-libraries while I actually decided to furnish a basic working set.
-If you need help with adding missing codecs, then I might be able to help you but you need to contact me by PM for further discussion.

Credits/Thanks:
-to the author of IJKPlayer: Zhang Rui (bbcallen)
-to @warwound (Martin) for permission to use (and modify) wrapper code for MediaController and OutlineTextView. The wrapper code for VideoView is based on code by myself and Martin.

PS: FFmpeg is actually a solution to record, convert and stream audio and video. It is used also for video and audio creation and manipulation. You can for instance download a YouTube-video and extract the audio-file. The version posted cannot do this since it only handles decoding/streaming. However, if I find some time later on, I will try to wrap a FFmpeg-library which will permit you to do other things and not only streaming(decoding).

Good luck!
 

Attachments

  • FFMpeg_b4a.zip
    105.9 KB · Views: 2,157
Last edited:

moster67

Expert
Licensed User
Longtime User
Continuing from the vlcb4a-thread

In the FFMpeg thread you write: "You can then add the missing containers, demuxers, muxers and codec by building and recompiling the FFmpeg-libraries yourself". I have no idea how to do that. Some more guidance would be appreciated.

Moved my answer to this thread.
Check the github project cited in the first post of this thread. Instructions can be found there and with some googling, testing, ripping of your hair, you will eventually get there :) I also had no idea when I started out but with patience I sorted things out.
 

Opengatebr

Member
Licensed User
Hello moster want to first thank all your work on that. I believe I have tested all alternatives Vitamio / VLCplayer and FFMpeg .

As you mentioned this be the safest for production will be you could help me solve this problem reported in the log without filter :

Could not find codec parameters for stream 1 (Audio : adpcm_g726 , 8000 Hz, 1 channels, 16 kb / s) : unspecified sample format .

I thank you strength and I'm sorry for bad English
 

moster67

Expert
Licensed User
Longtime User
Does the stream work with vitamio or VLC?
 

Opengatebr

Member
Licensed User
I flow with VLC vitamio I got with another IP camera not met me for remote access then changed by this current that does not flow in Vitamio too.
 

moster67

Expert
Licensed User
Longtime User
Not sure I understand your answer...
Can you please try answer again, maybe like this
Vitamio yes or no?
VLC yes or no?
 

moster67

Expert
Licensed User
Longtime User
Ok, I will check tomorrow. Now it's bedtime ;)
 

Opengatebr

Member
Licensed User
Thank you for your attention, you will help me a lot if we do this work.
I'll be looking forward to the test.
Good night friend :).
 

moster67

Expert
Licensed User
Longtime User
New version posted with updated so-files:
- FFmpeg_B4A_v3 - added support for some speech codecs suitable for IP-cams (https://dl.dropboxusercontent.com/u/47808624/FFMpeg_B4A_v3.zip)

Note: There is no API for setting buffer-size so streams from IP-cams take a little bit longer to load and start streaming. This is because IP cams mostly need small bit-rates and thus the data required for data-transfer is smaller and it takes longer to fill up the buffer.
 

ronovar

Active Member
Licensed User
Longtime User
I try this library and must say it is fantastic masterpiece...works fast, stable and have more powerful functions that's android native os player.

But i have one problem witch i think is bug:

I declare ffmpeg videoview and then to switch from full screen to small screen(preview) i use function SetLayout....when stream is start palying specificed width and height is automatically as stream width and height....

I need to when start playing video that stream video is fit into defined width and height.

B4X:
    FFMpegVideoView1.SetLayout(pnlVideo.Top - 2, pnlVideo.Width - 2, pnlVideo.Width - 4, pnlVideo.Height - 4)

On vlcb4a i use this code SetLayout and stream video is automatically scaled to defined Vlc width and height.

So if you can please fix this, so that stream video is scalled of defined width and height in SetLayout.

I try vlcb4 and setlayout and scaling works, but it is unstable and freezes offen. FFMpeg is much more smaller in size and faster.
 

Ezio Lepre

New Member
Licensed User
Longtime User
This lib also play audio files?
If so, how?
I tried with:

FFMpegVideoView1.SetVideoPath (File.Combine (File.DirAssets, "myfileaudio.mp3"))

but did not play.
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Great Mike.
I have this code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    FFMpegVideoView1.Initialize("FFMpegVideoView1")
    Activity.AddView(FFMpegVideoView1, 0, 0, 100%x, 100%y)
       
    FFMpegMediaController1.Initialize("FFMpegMediaController1")       
    FFMpegVideoView1.SetMediaController(FFMpegMediaController1)
    FFMpegVideoView1.SetVideoLayout(FFMpegVideoView1.VIDEO_LAYOUT_ZOOM)
               
    'FFMpegVideoView1.SetVideoPath("http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8")
    'FFMpegVideoView1.SetVideoPath(File.Combine(File.DirRootExternal, "test_video.mp4"))
    File.Copy(File.DirAssets, "waterfall.mp4", File.DirRootExternal, "waterfall.mp4")
    FFMpegVideoView1.SetVideoPath(File.Combine(File.DirRootExternal, "waterfall.mp4"))               
       

End Sub

If i try and SetVideoPath with http all work.
But if i set:
FFMpegVideoView1.SetVideoPath(File.Combine(File.DirRootExternal, "waterfall.mp4"))
i receive in log window:
FFMpegVideoView1_Prepared
and after few seconds the program in device close and crash.
Do you know why ? :confused:

Here project: https://www.dropbox.com/s/71oghf8qlnexjef/FFMpeg_Test.zip?dl=0

Thank you
Marco
 

moster67

Expert
Licensed User
Longtime User
I am unable to test now because I am at work. Will check this evening.
Anyway, there should be an error indication in the in the unfiltered logs?
 

MarcoRome

Expert
Licensed User
Longtime User
I am unable to test now because I am at work. Will check this evening.
Anyway, there should be an error indication in the in the unfiltered logs?

Hi Mike thaniks for your response. This is unfiltered logs:
bindService callerProcessName:com.google.android.gms, calleePkgName: com.google.android.gms, action: null
(HTTPLog)-Static: isSBSettingEnabled false
Tagging socket 191 with tag 60000000000{1536,0} uid -1, pid: 3675, getuid(): 10017
Indexing done B5626DF7300F8F655A4D93BA16D56C74BFDC6FFF
RegisterService intent:Intent { act=register_intent_action flg=0x10 cmp=com.google.android.talk/com.google.android.libraries.matchstick.net.SilentRegisterService } isPeriodic:false
Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
LogCat connected to: 3204d26b2f219123
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
FFMpegVideoView1_Prepared
--------- beginning of system
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
FFMpegVideoView1_Prepared

After that i have FFMpegVideoView_Prepared...boom crash app
 

moster67

Expert
Licensed User
Longtime User
There are more info in the unfiltered logs (at least on my device). The error is probably related to the codec of the video-files you attached but apparently not a codec-issue but something in the c++ code (error I get is "unknown FFP_MSG_xxx(700)"). Looking at the Github-project, I see that this error pops up some time (see also https://www.b4x.com/android/forum/t...for-b4a-decoding-streaming.44476/#post-271589) but I am afraid that there is nothing I can do since it seems to originate to FFMpeg source code. That said, other local video-files I have tried work fine.

I hope one day to wrap the latest version of the IJKPlayer and perhaps these problems have been resolved by the original author.

Try with some other video-files encoded in a different codec.
 

MarcoRome

Expert
Licensed User
Longtime User
There are more info in the unfiltered logs (at least on my device). The error is probably related to the codec of the video-files you attached but apparently not a codec-issue but something in the c++ code (error I get is "unknown FFP_MSG_xxx(700)"). Looking at the Github-project, I see that this error pops up some time (see also https://www.b4x.com/android/forum/t...for-b4a-decoding-streaming.44476/#post-271589) but I am afraid that there is nothing I can do since it seems to originate to FFMpeg source code. That said, other local video-files I have tried work fine.

I hope one day to wrap the latest version of the IJKPlayer and perhaps these problems have been resolved by the original author.

Try with some other video-files encoded in a different codec.
Thanks Mike.
 
Top