Mobile App Development: Working With Media Files on Android (Part 2)
In the previous blog post, we presented two solutions that work with media files on the Android Mobile App platform, especially videos files. In this post, I will present a guide for merging and trimming video files using MP4parser. All source codes can be found on GitHub.
The first thing that we must do before we start programing our mobile app is to download the mp4parser libraries from the project page right here. For this example, we will need ISOViewer and ISOParser, which is provided in the URL mentioned above.
Secondly, we will need to create a new project and add the libraries to the project. At the end, the project libraries must look like this:
Merging video files
The first code example that we will be working with is to merge video files. We must remember that working with media files types is an exhaustive job. We must implement all our classes in a separate thread so that we can leave the main visual application unfrozen.
Let’s start with our class skeleton. Create a new class for our mobile app projet called MergeVideos extending the superclass AsycTask. After creating the class, we can add the variables, constructor and required methods as follows:
As you can see our class must be initialized with 2 values, the working path folder and the list containing the video names, including the extension (e.g. video1.mp4, video2.mp4, videoN.mp4). The next step is to write the logic in charge of the video merging task. For this, we must follow the next algorithm:
1. For each video file we must create a Movie object containing all the information as video and audio tracks of such video.
2. For each video object, we must:
a) Get every video track and stack them in a list.
b) Get every sound track and stack them in a list.
3. Create a new Movie placeholder and add the movie and audio tracks stacked previously.
4. Write the new Movie object to disk, thus creating the new merged video.
So following the previous algorithm, the first task is performed by the following snippet:
Where count, is the number of videos to merge and inMovies is the array of Movie objects. The “for” loop will get all the video files references, generating the Movie objects and adding it to the array.
The next step is to read and concatenate the sound and video tracks, which is done by the following code:
After concatenating every video and audio track, we are ready to create the new movie placeholder for our mobile Android app using the snippet:
At this point, our new movie is created in a logical manner. Now, we must write this newly created movie into the nonvolatile memory, which can be done as follows:
And now our newly created merged video is ready to be reproduced!
Trimming video files
The next example is how to use this library to shorten the length of a given video. Like the previous example, all of the logic code must run on a separate thread, in order to keep the activity unlocked. The class skeleton should look like this:
The class is initialized with mediaPath which is the complete path to the video. The startTime, which is the start time in seconds, and length, which is the number of seconds it takes to trim the video file after the start time. In this case, all the logic is contained in the trimVideo method. So let us start with the logic algorithm:
1. For each track in the video, get the correct start time from all the tracks, since where are using mp4 files, this is a required process in order to trim the complete video.
2. For each track in the video, crop the track with a start and end sample time and add it to a new Movie object.
3. Write the newly created movie to the disk.
Following this algorithm, the first step can be performed by the following code:
In this case, the snippet is using a private function called correctTimeNoTextSyncSample to get the nearest synchronized sample to our desired start time. Afterwards, we are ready to crop each track of the video and the step 2 of our algorithm can be performed as follows:
After getting the correct start and end time sample from current track, we are ready to crop it and add it to the final movie object. After cropping each track of the mp4 file and concatenated it, we are ready to save the Movie object to disk. This step can be performed by:
And that’s it. We now have a newly cropped video created from a larger one!
As mentioned in the previous post, one of the main drawbacks of this library is that it can only work with MP4 files. Luckily for us, all android devices record video files in such format, so this is a good option when working with this kind of media. However, this library does not give support to transcode and/or compress media files, which in many cases, is an important task to do before uploading media files to the cloud. In this case our best option is to use ffmpeg which is our next topic of discussion. Till next time!
*Thanks to the mp4parser project for keeping this excellent library ().
*All codes were based on the demos provided by the library web page.
*More about the mp4 format here.
About the Author
Victor Cervantes is an Android developer with 1+ years of experience with the Android mobile platform. He has a Masters Degree in Computer Science specializing in optimization problem solving. He is an Appcelerator Titanium Certified Developer (TCD) and currently works at iTexico as an Android Developer.