Search code examples
androidaudioffmpegmp3android-ffmpeg

How to download a part of mp3 from server?


Use Case

My use case is roughly equal to, adding a 15-second mp3 file to a ~1 min video. All transcoding merging part will be done by FFmpeg-android so that's not the concern right now.

The flow is as follows

  • User can select any 15 seconds (ExoPlayer-streaming) of an mp3 (considering 192Kbps/44.1KHz of 3mins = up to 7MB)
  • Then download ONLY the 15 second part and add it to the video's audio stream. (using FFmpeg)
  • Use the obtained output

Tried solutions

  • Extracting fragment of audio from a url

    RANGE_REQUEST - I have replicated the exact same algorithm/formula in Kotlin using the exact sample file provided. But the output is not accurate (± 1.5 secs * c) where c is proportional to startTime

  • How to crop a mp3 from x to x+n using ffmpeg?

    FFMPEG_SS - This works flawlessly with remote URLs as input, but there are two downsides,

    1. as startTime increases, the size of downloaded bytes are closer to the actual size of the mp3.
    2. ffmpeg-android does not support network requests module (at least the way we complied)

So above two solutions have not been fruitful and currently, I am downloading the whole file and trimming it locally, which is definitely a bad UX. I wonder how Instagram's music addition to story feature works because that's close to what I wanted to implement.


Solution

  • Its is not possible the way you want to do it. mp3 files do not have timestamps. If you just jump to the middle of an mp3, (and look for the frame start marker), then start decoding, You have no idea at what time this frame is for, because frames are variable size. The only way to know, is to count the number of frames before the current position. Which means you need the whole file.