3

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.

Sharukh Mohammed
  • 365
  • 2
  • 16
  • 1
    You need a server which you can tel that you need eleven seconds of a file starting from a certain time. Thats all. – blackapps Aug 19 '20 at 09:03
  • Do you think there's no client-side only implementation of the same? – Sharukh Mohammed Aug 19 '20 at 09:58
  • Dont think so. If the server only serves complete files than a client should start downloading from the start. But of course it can stop downloading after starttime+interval. – blackapps Aug 19 '20 at 10:01

1 Answers1

3

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.

szatmary
  • 29,969
  • 8
  • 44
  • 57