I looking for any SDK-LEVEL(without using ffmpeg) approach for compress video programmatically in android.
I can't use ffmpeg cause its size overhead.Is there any lightweight solution for that ?
I want reduce size of video before uploaded to server (i can't modify server upload limit) so if you have any idea other than compressing video please share it.
note : my min-sdk version is 14
Related
I want to convert all videos based on h265 to h264 and at the same time reduce the resolution to for example 720p to avoid working on very big resolutions and later uploading that kind of big size files.
I see docs https://developer.android.com/guide/topics/media/media-formats says that for h265 Android OS supports only decoding, not encoding.
I know that FFMPEG will solve all my problems, but including FFMPEG will increase the app size very much, I'd like to avoid that. I am trying to use currently the Android MediaCodec, but it looks like it would work fine with converting h264 to h264 but not with h265 to h264.
Do you have any ideas? I don't need to support old Android versions.
Thanks for any advice.
If you're compiling it for Android 12 or higher, you can use the built-in transcoder
Otherwise, you'll need to include a 3rd-party media transcoder library, and FFMPEG is still your best choice
Hi I am trying to trim video in android, but all of the source codes I have found are using ffmpeg, is there a smaller library, which I can use ?
Because ffmpeg library is about 8-9 MB, and my application is about 6 MB, adding ffmpeg library to my app will make it more than double size.
You can do this with mp4parser library. Have a look at the ShortenExample it does exactly what the name suggests. Since the library cannot re-encode the video it can only cut the video at I-frames. So the points in time where you can make a cut are quite coarse.
On Android 4.1 you can access the hardware codecs via MediaCodec API which could be an option (but I haven't seen any example of that yet)
Or, you can use this class: TrimVideoUtils.java
I'm not an expert in Video Editing but what I want to understand the logic of Whatsapp video processing.
First of all I have noticed that whatever the file is, Whatsapp sets the limit of Uploaded videos to 16MB, after which whatsapp crops the video to not exceed the limit. is this a convention or it's a personal choice ?
Secondly, When a video is recorded using the Camera it's not compressed by default, so whatsapp compresses it using FFMPEG I guess, and it takes no time. (tried for a video of 1min 1920x1080 with 125MB of size, becomes 640x360 with 5MB of size in no time, and the upload starts automatically).. how may they do this ? and why the choice of 640x360, It seems to me very fast for 2 asynchronous tasks : Compression + Upload.
When I run the compression command ffmpeg -y -i in.mp4 -codec:v libx264 -crf 23 -preset medium -codec:a libfdk_aac -vbr 4 -vf scale=-1:640,format=yuv420p out.mp4 it takes approximatively 1 min and the video is being rotated !! :D
Finally, when we download a video from Youtube it's already compressed (I guess) and whatsapp doesn't even try to compress it. So I think that it automatically detects thats the video is compressed. How can we detect this ?
Thank you.
Here are possible answers to your questions:
Quest. 1: Its a personal choice. The whatsapp team is trying to offer the best User Experience (UX) they could to users of their app, that is why they have kept a limit of 16MB for video file. Imagine how long it would take to upload a file of about 125MB. Hence, the app compresses the file for quicker upload and seamless experience.
Quest. 2: I guess you already answered this question yourself - Asynchronous programming. The large video file you feed it, gets encoded into a compressed format according to the algorithm they have written for the app. As Devs., we all know about algorithms and we all know there are things you can do to speed up execution. I guess they implemented their very own algorithm using Asynchronous programming that speeds up the process. The ffmpeg library you mentioned i guess was coded in C which i think doesn't support async call (not so sure though). After this, upload takes over.
Quest. 3&Finally: Codecs are standards. If you encode a video file to MPEG4, then try to re-encode it again to MPEG4 even using another program, you will get the same result as far as both programs are using same encoding standards, i.e. they didn't implement a specific algorithm for their programs(this takes years of work). So, when your Whatsapp tries to encode the file, it gives the same result.
Hope I have been able to answer your questions.
MichVeline
Use media codec instead of ffmpeg for better performance. If your use case is only to compress the video, MediaCodec would be best option for android. It helps you to write the code asynchronously and also gives you lots of freedom to optimize you algorithm.
I am trying to upload the video file to server , for small size video like 4 to 5 mb it works fine , but if when i am trying to upload the video more than 15 to 20 mb , it takes too much time for upload , that's why i want to compress the video , is there any library or tool available for compressing video before uploading to server.
thanks.
There are few approaches that you can take to upload larger size video on to the server:
1) You can make use of standard way of recording it across all mobiles which give you standard size:
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setVideoEncodingBitRate(690000 );
2) Make use of ffmpeg or work around with it, it might help to compress video.
https://github.com/halfninja/android-ffmpeg-x264
3) (Recommended) Compressing the video according to me will degrade the quality of video to far worse stage, so what the other work around and best which majorly used is:
Make a Byte array of the video
Make small Chunks of that byte array
Make a Web-service on Server reads this Chunk one by one, send it in queue.
Once received on server make a Video file out of it again.
Runs this Operation in Background async or as service anyways. So it doesnt effect foreground process. Once its done Just Notify!!
Hope this will help
You can try use the FFMpeg library.
It's easy way to compress videos.
Can you check the site https://www.ffmpeg.org/ for more information, or post as comment what are you need to compress.
If you change CRF with the command -crf "15~25". It's a good compress video with good quality.
I've a requirement where I need to transcode small video clips shot from Native camera app to lower bitrate/resolution Mp4 which is shreable via email etc.
What is the best way to transcode/convert the video on device itself. FFMPEG or any other library?
p.s. I know this is an overkill for the device but client leaves me with no option. He doesn't care about battery or time it takes. I'm targeting this for quad-cores, where CPU is not a problem.
Your best bet would be to use something like ffmpeg which has been ported to Android (see this SO post: ffmpeg for a android (using tutorial: "ffmpeg and Android.mk") and the ffmpeg port for android which is here: http://bambuser.com/opensource). You'll have to use JNI etc, but that will save you the hassle of dealing with the byte stream yourself.
Haven't tried it on Android myself, so YMMV:
Is there a Java API for mp4 files?
http://code.google.com/p/mp4parser/
If you're recording on-device, why not set the expected format from your code? It appears the api lets you set video size, framerate etc. in the MediaRecorder class.