Is there anything that does the opposite of a MediaExtractor on android?
Take one or multiple streams from MediaCodecs (e.g. 1 video and 1 audio)
and package them in a container format for streaming or writing to files?
Looks like the answer is no.
Mostly because of the underlying API is designed for video streaming and not for video compression.
Writing encoder's output to file you'll get raw h264 file. Which can be played using mplayer or ffplay for example.
Also ffmpeg can be used to mux this raw file into some container. But first of all you need to build ffmpeg for android.
Related
Since MediaRecoreder not supports encoding to mp3 format.
Can ffmpeg to record audio directly from Android audio source (how to specify this) or it only encodes one file to other format?
You mean using Android to record audio and save it as mp3?
If so, you can code mp3 using libmp3lame. And this is very useful.
Since Android Record outputs a pcm, liblmp3lame can use pcm as input and then output mp3 data.
There is some good documentation on this site called big flake about how to use media muxer and mediacodec to encode then decode video as mp4, or extract video then encode it again and more stuff.
But it doesnt seem that there is a way to encode audio with video at the same time, no documentation or code about this. It doesn't seem impossible.
Question
Do you know any stable way of doing it that will work on all devices greater than android 18?
Why no one implemented it, is it hard to implement?
You have to create 2 Mediacodec instances, one for video and one for audio and then use MediaMuxer to mux the video with audio after encoding, you can take a look at ExtractDecodeEditEncodeMuxTest.java and at this project to capture camera/mic and save to mp4 file using Mediamuxer and Mediacodec
I have found a solution for resampling an .mp4 video taken with the camera on the device to make it smaller (resizing by resolution, bitrate, and framerate). The problem is, it doesn't carry the audio over.
I have looked at several different options for trying to get the audio out of my source (large) mp4 and push it into my smaller mp4 and I can't not seem to get any of these procedures to work correctly.
I've tried the following:
1) extracting the PCM audio from the source using: How do I extractor audio to mp3 from mp4 using java in Android?
2) converting the PCM to M4A and then adding the M4A to the smaller MP4 using: https://github.com/tqnst/MP4ParserMergeAudioVideo/blob/master/Mp4ParserSample-master/src/jp/classmethod/sample/mp4parser/MainActivity.java
that's the method I got closest with but the audio was really slow and didn't match up at all with the video in the smaller mp4.
I also tried a "direct copy" from one mp4 to the other with a variation of this: Concatenate multiple mp4 audio files using android´s MediaMuxer
that made my smaller mp4 actually larger (in file size) than my source mp4 and it didn't actually move the sound over.
The android documentation for MediaMuxer is pretty terrible and I can't make heads or tails of what I need to do to get this to work. It seems like it should be a pretty trivial task....
any suggestions or advice would be greatly appreciated.
TIA
I ended up just using ffmpeg with this solution:
https://github.com/WritingMinds/ffmpeg-android-java
I am trying to use x264 on Android to build a video system, which could do the encoding and decoding work. I've finished the video data capturing and encoding work. But I am not sure about how to deal with the audio stuff. Does x264 support audio recording? Or should I use some other api provided by Android? If I do the video capturing and audio capturing job seperately, how could I make sure they are time synchronization?
The x264 is only a video codec. For audio you need to use some audio codec (for example aac could be used here). Having these you can merge them into output file (.mkv or whatever you have).
https://sites.google.com/site/linuxencoding/x264-encoding-guide
I have a Vorbis stream that I can decode to PCM if necessary, and I have a raw h264 stream all three of which are supported by Android when in a container. Wondering if there is any way to manually feed video and audio samples into the android MediaPlayer without any container. I would imagine I would have to override methods within the MediaPlayer. Does anyone have experience with this or have an easier way to do this? I can't imagine its impossible...
You may be able to play the audio pcm samples (For that also, I guess you may have to put a wav header) but you may not be able to play the H264 elementary stream without a container. In the media framework (Stagefright), there are sniffers functions registered for various container formats which could tell what kind of parser need to be used and then extractor(parser) is created. I don't think that you will be able to play the H264 elementary stream from the application using the inbuilt media framework of Android.