MP4 with MP3 via HLS on Android - HTTP Live Streaming

posted by sacah on

If you have a MP4 with a AAC audio stream, both iOS and Android play it fine via the HTML5 Audio player over HLS. But if you use a MP4 with a MP3 audio stream, iOS plays fine, but Android won't play.

Turns out Android only plays as long as there is a video stream, so we need to create a video stream to go with our audio. The easiest method of this is with ffmpeg.

1. Create a blank 32x32 image, flat white, or black or any other color, I called mine image.jpg.

2. Create a looping video the length of the audio, or longer than the longest MP3 you'll be using.

ffmpeg -loop 1 -i image.jpg -vcodec libx264 -vprofile baseline -level 3 -t 360 loopVideo.mp4
This will loop the image.jpg for 360 seconds, and save the output as a H.264 video stream called loopVideo.mp4. The video is created at Baseline 3, so this will work on iOS 3+ devices.

3. Combine the MP3 with the loopVideo.mp4

ffmpeg -i loopVideo.mp4 -i "My MP3 Song.mp3" -c copy -map 0:v -map 1:a:0 -vprofile baseline -level 3 -shortest finalSong.mp4
This combines the video with the audio, it is a straight copy from both video and audio, so it occurs in 30ms or so. -shortest tells ffmpeg to clip the streams at the shortest one. If you made the video longer than all your MP3s, ffmpeg will clip the video stream to the length of the MP3 you're using.

Now your right to stream this via HLS onto Android. Let me know if you have any issues with this, or find better methods.