Is there possibility to compress audio and video file in android programmatically? - android

As I record the video file for 1min it take 3mb of size as i want to post this file to server it is taking more time.So I need to decrease the time by compassing the recorded video file and send it to server.Is there possibility to compress the video file programmatically.
Thanks in advance.
Teja.

Note that simply compressing it with some zip-algorithm (relatively easy to do in Java) wouldn't help you much. Try compressing the file yourself. (If it did make the file smaller, it would most likely already be a part of the video-file format, wouldn't it :-)
If you want to compress the video, you have to re-encode it with a lower bit-rate / lower resolution or something similar.

Related

Transcode video to lower bitrate and stream

I have a working app that streams video to Chromecast(using nannoHttpd) and everything is working fine. Now my problem is: videos recorded using new devices are too large in size to stream, so I want to re-encode videos to some lower bitrate.
I tried ffmpeg but the results are not satisfactory and it will increase the apk size by 14 MB.
Now I am trying the MediaCodec api. It is faster than ffmpeg, but it takes the input file and writes it to the output file and I want to re-encode byte data that is to be served by nannohttpd.
Now a solution comes to my mind, that is to transcode the video and stream the output file but its has two drawbacks;
What if the file is too large and the user doesn't see the whole video? Much of CPU, battery resource is wasted.
What if the user fast forwards a long video to a time which is not re-encoded yet?
1 MediaCodec just do one thing decode encode! and you will get raw bytes of new encoded data. So it is up to the programmer to choose to either dump that into a container (.mp4 file) using a muxer. So no need here to rewrite everything back into a file.
2 Seek to the proper chunk of data and restart MediaCodec.

Compress video like whatsapp

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.

How to reduce size / quality of video file Android

I have been looking for solution here and google but I didn't find it.
I want to reduce video file from camera or gallery to send it to server.
please help me

compress video file before uploading to server

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.

How to keep the audio size, reduced, while recording in android?

My requirement is to keep the size of audio as small as i can. Also, after getting the recorded stream, i can accomplish this task by zencoder type APIs but I am interested to do this thing while recording.
Thanks
Talha
MediaRecorder supports AMR encoding which is very efficient. The size of the resulting file will grow at 1600 bytes/second.

Categories

Resources