tanersener / mobile-ffmpeg

FFmpeg for Android, iOS and tvOS. Not maintained anymore. Superseded by FFmpegKit.
https://tanersener.github.io/mobile-ffmpeg
GNU General Public License v3.0
3.88k stars 795 forks source link

Reduce APK File Size #428

Closed sureapps closed 3 years ago

sureapps commented 4 years ago

Mobile - FFMPEG When i use this library my apk file size is 84 MB so can you suggest me how to reduce file size and unnecessary file remove.

i don't need this ffprobe features in my application so how to i remove this .so file and other unnecessary file

tanersener commented 4 years ago

See How to decrease binary size.

alexcohn commented 4 years ago

Open your APK with Build/Analyze APK… from the Android Studio menu. This will show all contents of your app sorted by size. Under lib, you can find the .so files that belong to mobile-ffmpeg. For me, they all sum up to 6MB. If yours differ significantly, publish the contents of this folder, I will try to help you reduce its size.

lucazin commented 4 years ago

Hi @alexcohn !

Can u help me with this question about apk size? Can i put a picture of content here?

This is my lib folder

image

alexcohn commented 4 years ago

You could consider using some smaller package (e,g, https://github.com/tanersener/mobile-ffmpeg/releases/download/v4.3.2/mobile-ffmpeg-min-4.3.2.aar), but I don't know which codecs and other extension libraries your app require. Anyways, your list looks legit, and all these libraries sum up to 36 MB, less than half of your full APK. This is quite a fair proportion, in my eyes.

Note that you could make use of app bundle dynamic features: delay installation of non-essential part of your native libraries until the end-user really needs them.

If you use ffmpeg libraries from C++, you can omit libmobileffmpeg.so (not a significant gain), but also you can build the ffmpeg libraries as static, and let the linker drop any unused pieces of ffmpeg.

lucazin commented 4 years ago

I just need compress video.. Nothing more..

This aar file 25mb will increase 25mb in apk?

Do you have some performance command just for compress a 60 seconds video file?

alexcohn commented 4 years ago

The AAR I linked above will replace the 18.1 MB libavcodec.so with a version which is about 50% of that. But probably, it does not include your preferred video encoder, and you will be best served if you build your own libraries, enabling only that encoder.

The size of AAR should not frighten you: Android Studio will unpack it and extract only relevant pieces for your APK.

just for compress a 60 seconds video file

Do you intend to send it somewhere? What are your requirements of speed of compression vs. visual quality? What's the resolution?

The fastest way to create a video on Android is by not using ffmpeg at all, but rather the platform MediaMuxer and MediaCodec, which uses the hardware encoder built into every Android device.

lucazin commented 4 years ago

Do you intend to send it somewhere? What are your requirements of speed of compression vs. visual quality? What's the resolution?

About mediamuxer for compress video instead ffmpeg, do you have some example in android? If you said for me its better i will try.

Em seg., 25 de mai. de 2020 às 18:35, Alex Cohn notifications@github.com escreveu:

The AAR I linked above will replace the 18.1 MB libavcodec.so with a version which is about 50% of that. But probably, it does not include your preferred video encoder, and you will be best served if you build your own libraries, enabling only that encoder.

The size of AAR should not frighten you: Android Studio will unpack it and extract only relevant pieces for your APK.

just for compress a 60 seconds video file

Do you intend to send it somewhere? What are your requirements of speed of compression vs. visual quality? What's the resolution?

The fastest way to create a video on Android is by not using ffmpeg at all, but rather the platform MediaMuxer and MediaCodec, which uses the hardware encoder built into every Android device.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tanersener/mobile-ffmpeg/issues/428#issuecomment-633722623, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGXVDFRPFS5W6Z2DABVP5ZLRTLQDLANCNFSM4NGTBVXA .

alexcohn commented 4 years ago

'better' us a delicate term. With H.265, you can get better quality for same video size. With VP8, you can get a video that can play on a wide variety of targets.

AamirNaseer1 commented 4 years ago

Open your APK with Build/Analyze APK… from the Android Studio menu. This will show all contents of your app sorted by size. Under lib, you can find the .so files that belong to mobile-ffmpeg. For me, they all sum up to 6MB. If yours differ significantly, publish the contents of this folder, I will try to help you reduce its size.

help me to reduce its size

ffmpeg

lucazin commented 4 years ago

Can u tell what code i have to use? Or if you can help me zipping the aar just for ffmpeg because im really lost in this ..and i really use this lib

But the APK i huge bigger. =/

Em qua., 5 de ago. de 2020 às 09:35, AamirNaseer1 notifications@github.com escreveu:

Open your APK with Build/Analyze APK… from the Android Studio menu. This will show all contents of your app sorted by size. Under lib, you can find the .so files that belong to mobile-ffmpeg. For me, they all sum up to 6MB. If yours differ significantly, publish the contents of this folder, I will try to help you reduce its size.

help me to reduce its size

[image: ffmpeg] https://user-images.githubusercontent.com/50737756/89413263-6347fc80-d6dd-11ea-84c2-7bc6e0939b5f.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tanersener/mobile-ffmpeg/issues/428#issuecomment-669167359, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGXVDFT3WCCCRTK7YJHYQRLR7FGZ3ANCNFSM4NGTBVXA .

alexcohn commented 4 years ago

Currently, there is not much you can do if you must support all devices from API 16 in a single APK. It is recommended to use split APK (see the test app here that produces 4 different APKs for each family of CPUs). If you don't distribute the APK through Play Store, you can drop the arm64-v8a libraries. Furthermore, if you can set your minSdkVersion to 21 or higher, you can reduce the size of the armeabi-v7a APK by only keeping the -neon variants of the libraries.

abkoradiya commented 4 years ago

You could consider using some smaller package (e,g, https://github.com/tanersener/mobile-ffmpeg/releases/download/v4.3.2/mobile-ffmpeg-min-4.3.2.aar), but I don't know which codecs and other extension libraries your app require. Anyways, your list looks legit, and all these libraries sum up to 36 MB, less than half of your full APK. This is quite a fair proportion, in my eyes.

Note that you could make use of app bundle dynamic features: delay installation of non-essential part of your native libraries until the end-user really needs them.

If you use ffmpeg libraries from C++, you can omit libmobileffmpeg.so (not a significant gain), but also you can build the ffmpeg libraries as static, and let the linker drop any unused pieces of ffmpeg.

Hey, can you tell me how can I only include this library in dynamic features?

solankiranvir commented 4 years ago

Hello any update to reduce apk size? like in other ffmpeg lib i use to avoid some feature like this : aaptOptions { ignoreAssetsPattern "!*ffprobe" } Put this line in main gradle and it avoid ffprobe tool so apk size is reduced. So i want to ask you is there any this type of installment to avoid unwanted tools or .so files for reduce APK size? I use only Delogo feature in my app. Thanks

ghost commented 4 years ago

Hey @alexcohn could you please tell how to drop armeabi-v8a , x86_64 , x86 , mips64 , mips, I only want to use armeabi-v7a in my app. Rest all are unnecessary and increasing my apk size. file size

HBiSoft commented 4 years ago

@raghavtilak You have two options. First is rebuilding FFmpeg with custom options:


Or if you do not want to rebuild it, you can just add the following in your manifest:

android {
    ....
    defaultConfig {
        ....
        ndk {
            abiFilters "armeabi-v7a"
        }
    }
}

Hope this helps.

alexcohn commented 4 years ago

For mobile-android, I would recommend to use arm-v7a-neon, it's significantly faster.

HBiSoft commented 4 years ago

@alexcohn I did a quick test this morning. I used the same input video with the same command. I tested both arm-v7a and arm-v7a-neon and I couldn't see much of a difference. arm-v7a was actually faster by 1200ms, which was unexpected.

Even stranger is that my device has a Cortex-A53 @1,59GHz, according to arm.com it's an arm64-v8a processor. But when I test it, it always selects arm-v7a or arm-v7a-neon(if available).

alexcohn commented 4 years ago

arm-v7a was actually faster by 1200ms, which was unexpected.

really unexpected. Thanks for not taking my advice blindly. The performance gain depends highly on what kind of codecs you use, only few have neon optimization.

As for arn64, check adb shell getprop ro.product.cpu.abilist.

ghost commented 4 years ago

@HBiSoft Simply doing this doesn't reduce my apk size. android { .... defaultConfig { .... ndk { "armeabi-v7a" } } }

And I am still not getting your first method. Like where do I find android.sh file?

All i have done is adding arthenica ffmped-full-gpl in my gradle build. And when I make the apk it includes all(armeabi-v7a,v8a,x86,x86_64 etc) and i want only armeabi-v7a.

Could please help me find a way out to do so.

HBiSoft commented 4 years ago

@raghavtilak

Simply doing this doesn't reduce my apk size.

It should since you are only packaging armeabi-v7a

All i have done is adding arthenica ffmped-full-gpl in my gradle build. And when I make the apk it includes all(armeabi-v7a,v8a,x86,x86_64 etc)

If you added android { .... defaultConfig { .... ndk { "armeabi-v7a" } } }, you should only have armeabi in your lib folder. You can confirm this by changing your apk to zip, for example YourApp.apk to YourApp.zip. Then extract it and look in the lib folder. You should only have a armeabi-v7a folder.

And I am still not getting your first method. Like where do I find android.sh file?

In the main directory of this library. Download this library and you should see the android.sh in the main directory.

alexcohn commented 4 years ago

@raghavtilak please post your build.gradle – there may be too many things that may go wrong

ghost commented 4 years ago

`apply plugin: 'com.android.application' android {

signingConfigs {
    release {
        storeFile file('D:\\Indiyo\\key.jks')
        storePassword 'indiyo@123'
        keyAlias = 'keystore'
        keyPassword 'indiyo@123'
    }
}
compileSdkVersion 30

defaultConfig {
    applicationId "com.indian.IndiYo"
    minSdkVersion 24
    targetSdkVersion 30
    versionCode 10
    versionName "2.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    ndk {
       // abiFilters "armeabi-v7a","x86","x86_64"
        "armeabi-v7a"
    }
}

compileOptions {
    targetCompatibility JavaVersion.VERSION_1_8
}
// Sceneform libraries use language constructs from Java 8.
// Add these compile options if targeting minSdkVersion < 26.
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

buildTypes {
    release {

        lintOptions {
            checkReleaseBuilds false
            abortOnError false
        }

        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        //Other parameters
        debuggable false
        jniDebuggable false
        renderscriptDebuggable false
        pseudoLocalesEnabled false
        zipAlignEnabled true
    }
}

}

dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleCompatible implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.exifinterface:exifinterface:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.material:material:1.0.0' testImplementation 'junit:junit:4.12' implementation 'androidx.multidex:multidex:2.0.0' implementation 'com.googlecode.mp4parser:isoparser:1.1.21' implementation 'androidx.percentlayout:percentlayout:1.0.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.google.android.exoplayer:exoplayer:2.8.3' implementation 'com.facebook.android:facebook-android-sdk:4.26.0' implementation 'com.gmail.samehadar:iosdialog:1.0' implementation 'com.github.ybq:Android-SpinKit:1.2.0' implementation 'net.the4thdimension:audio-wife:1.0.3' implementation 'com.github.MasayukiSuda:GPUVideo-android:v0.1.0' implementation 'de.hdodenhof:circleimageview:3.0.0' implementation 'com.makeramen:roundedimageview:2.3.0' implementation 'com.mindorks.android:prdownloader:0.6.0' implementation 'com.akexorcist:RoundCornerProgressBar:2.0.3' implementation 'com.google.android.material:material:1.3.0-alpha01'

implementation 'org.florescu.android.rangeseekbar:rangeseekbar-library:0.3.0'
implementation 'commons-io:commons-io:2.5'

// Provides ArFragment, and other Sceneform UX resources:
implementation "com.google.ar.sceneform.ux:sceneform-ux:1.15.0"

implementation 'com.android.volley:volley:1.1.1' //edited by  bhaskar
implementation 'petrov.kristiyan:colorpicker-library:1.1.10'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.airbnb.android:lottie:2.7.0'

//Do not Update this library their is a camera issue with new version
implementation 'com.wonderkiln:camerakit:0.13.0'

implementation 'com.google.android.gms:play-services-auth:18.0.0'
implementation 'com.google.firebase:firebase-database:19.3.1'
implementation 'com.google.firebase:firebase-messaging:20.2.1'
implementation 'com.google.firebase:firebase-core:17.4.3'
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.google.android.gms:play-services-ads:19.2.0'
implementation 'com.google.firebase:firebase-dynamic-links:19.1.0'
implementation 'com.google.firebase:firebase-analytics:17.4.4'
implementation 'com.google.firebase:firebase-crashlytics:17.1.1'
implementation 'com.google.firebase:firebase-perf:19.0.8'

implementation 'com.arthenica:mobile-ffmpeg-full-gpl:4.2.2.LTS'
implementation 'com.squareup.okhttp:okhttp:2.7.5'
// request permission
implementation 'com.karumi:dexter:4.2.0'

implementation 'com.github.MasayukiSuda:GPUVideo-android:v0.1.0'
implementation 'jp.co.cyberagent.android:gpuimage:2.0.3'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

implementation('com.giphy.sdk:core:1.0.2@aar') {
    transitive = true
}

implementation 'com.chauthai.overscroll:overscroll-bouncy:0.1.1'
implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.3.0'
implementation 'com.github.danylovolokh:hashtag-helper:1.1.0'
implementation 'com.mindorks.android:prdownloader:0.6.0'

implementation 'com.facebook.shimmer:shimmer:0.4.0'

implementation 'com.facebook.fresco:fresco:2.1.0'

implementation 'com.appyvet:materialrangebar:1.4.6'

androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

implementation 'com.danikula:videocache:2.7.0'

implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.google.code.gson:gson:2.3.1'

} apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.firebase.crashlytics' apply plugin: 'com.google.firebase.firebase-perf' apply plugin: 'com.google.ar.sceneform.plugin'

sceneform.asset('sampledata/girlglass.fbx', // 'Source Asset Path' specified during import. 'default', // 'Material Path' specified during import. 'sampledata/girlglass.sfa', // '.sfa Output Path' specified during import. 'src/main/res/raw/girlglass') ` @alexcohn here's my build.gradle

ghost commented 4 years ago

@HBiSoft I don't know why it's not working in my case. You can see these libs are still there in apk. Also I have Zipped and Unzipped but there is no reduction in apk size.

This is the new apk Analyze apksize

HBiSoft commented 4 years ago

@raghavtilak it should be:

ndk {
    abiFilters "armeabi-v7a"
}

It is important to remember that you should support 64bit when releasing to Google Playstore. So, by doing this, you will not be able to release your app on Google Play Store.

ghost commented 4 years ago

@HBiSoft Oh that worked!! Thank you so much. Yeah I read that and due to this I was in a dilemma that whether I should go with armeabi-v7a or armeabi-v8a? I read in an article that phones manufactured in year 2010 to 2014 used armeabi-v7a, after that all devices use armeabi-v8a Will this work? I mean after adding x86_64 will I be able to release my app on Google Play Store? ndk { abiFilters "armeabi-v7a","x86_64" }

HBiSoft commented 4 years ago

@raghavtilak I would rather go with ndk { abiFilters "armeabi-v7a", "arm64-v8a" }. There is not a lot of devices that runs on x86.

alexcohn commented 4 years ago

Yes, abiFilters "armeabi-v7a","x86_64" or even abiFilters "x86_64" is enough to qualify for Play Store remove the Gradle warning This app only has 32-bit native libraries. But it may still fail when you try to publish the APK. Another question, very few devices can run "x86_64", and it may be considered cheating if your actual audience mostly uses 64-bit capable ARM.

Finally, you can safely use splits to keep the APK download size smaller:

android.splits {
    abi {
        enable true
        reset()
        include 'armeabi-v7a', 'arm64-v8a', 'x86'
        universalApk false
    }
}
ghost commented 4 years ago

Thank you so much for showing your concern and helping me out with this issue @alexcohn and @HBiSoft . I am very much obliged.

datcoind commented 4 years ago

I see there are many os files. Can I delete the file "libavcodec_neon.so" to reduce the space, and does it affect my application. I use ffmpeg mobile to trim audio and merge audio with video, and how to delete the above file. Thanks. @alexcohn @raghavtilak a

ghost commented 4 years ago

The NDK supports ARM Advanced SIMD, commonly known as Neon, an optional instruction set extension for ARMv7 and ARMv8. Neon provides scalar/vector instructions and registers (shared with the FPU) comparable to MMX/SSE/3DNow! in the x86 world.

Not all ARMv7-based Android devices support Neon, but devices that do may benefit significantly from its support for scalar/vector instructions.

All ARMv8-based devices support Neon. Source: https://developer.android.com/ndk/guides/cpu-arm-neon

So I don't think deletion of these files would affect the app. The only thing is that neon speeds up the process. So the speed of process that you might have got from neon , you won't be getting.

alexcohn commented 4 years ago

Actually, ARM devices that don't support NEON are extinct these days, so you can almost as safely remove the non-NEON arm32 variants. The 64-bit ARM always comes with NEON built in. As I explained earlier, the rules of the Play Store require that you have 64-bit code. Luckily, you don't need a huge APK to support both armeabi-v7a and arm64-v8a. You can build splits, or even better: Android Bundle.

tusharpingale04 commented 4 years ago

@alexcohn As suggested, When i try to upload AAB, it gives me an error without any message. I am not able to figure out what's wrong in this case?

alexcohn commented 4 years ago

@tusharpingale04: 😕 so many weird things may go wrong with Android Bundles. But sometimes they simply work. At any rate, most likely your error is not related to this library. I suggest that you start with something really simple, and add components step by step.

josebraz commented 4 years ago

@alexcohn How to remove "armeabi-v7a/libavcodec.so" and still have "armeabi-v7a/libavcodec-neon.so" on the apk generated?

Actually, ARM devices that don't support NEON are extinct these days, so you can almost as safely remove the non-NEON arm32 variants. The 64-bit ARM always comes with NEON built in. As I explained earlier, the rules of the Play Store require that you have 64-bit code. Luckily, you don't need a huge APK to support both armeabi-v7a and arm64-v8a. You can build splits, or even better: Android Bundle.

alexcohn commented 4 years ago

@josebraz Actually, in mobile-ffmpeg-min-4.4.aar you won't find armeabi-v7a/libavcodec.so, only jni/armeabi-v7a/libavcodec_neon.so.

If you are building the AAR yourself, run zip -d mobile-ffmpeg-*.aar jni/armeabi-v7a/libavcodec.so and so on.

abkoradiya commented 4 years ago

Has someone done the full split configuration for android app bundles? That would really be the ideal setup for this (as opposed to abi filters which leads to unsupported devices, even if only a few).

Please share if anyone has the aab configuration setup 😁

No need of any setup. just build aab. that's it.

appsoft124 commented 3 years ago

Yes, abiFilters "armeabi-v7a","x86_64" or even abiFilters "x86_64" is enough to qualify for Play Store remove the Gradle warning This app only has 32-bit native libraries. But it may still fail when you try to publish the APK. Another question, very few devices can run "x86_64", and it may be considered cheating if your actual audience mostly uses 64-bit capable ARM.

Finally, you can safely use splits to keep the APK download size smaller:

android.splits {
    abi {
        enable true
        reset()
        include 'armeabi-v7a', 'arm64-v8a', 'x86'
        universalApk false
    }
}

this method is working but 3 signed apk file buiilding which one we will upload on play store

alexcohn commented 3 years ago

this method is working but 3 signed apk file buiilding which one we will upload on play store

You must upload at least the arm64 flavor. To support more devices, you will also upload the armeabi-v7a. The x86 flavor has limited application in production (it may be very useful during development and testing, it will work on your emulator).

At any rate, you must follow the guidelines to set up the version codes for each flavor wisely: so that each user receives the the correct flavor with each upgrade.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.