[FFmpeg-user] How can I get byte-accurate segments?

Paul B Mahol onemda at gmail.com
Fri Feb 12 11:02:32 CET 2016


On 2/12/16, Moritz Barsnick <barsnick at gmx.net> wrote:
> On Thu, Feb 11, 2016 at 17:19:47 -0700, Grant Curtis wrote:
>> I'm working with a Speech-to-text service that wants audio files that are
>> exactly 160kb large, which means 80,000 16-bit samples
>
> Which makes this uncompressed "raw audio", basically.
>
>> So far I've converted my video stream into an audio file with a sampling
>> rate of 16,000 and converted it to s16le. Now I just have to grab 80,000
>> samples out of it.
>> Is there any way to do that? Or perhaps I need to begin with a source file
>> in a specific bitrate so that the math lines up?
>
> 16000 samples per second, a total of 80000 samples, that makes exactly
> five seconds, right? Regardless of your input, it should be no issue
> telling ffmpeg this length: "-t 5".
>
> On the other hand, you don't mention the word "segment" in your text,
> just in the subject. Do you want to take an input file of arbitrary
> length, and split it into segments of exactly five seconds each? You
> should probably have a look at the segment muxer then:
> https://www.ffmpeg.org/ffmpeg-formats.html#segment_002c-stream_005fsegment_002c-ssegment
>
> I fail to get constant length output there though. Hmmm. I have no idea
> why. With videos and keyframes and the likes, I understand the issue.
> Not here.
> $ ffmpeg -t 35 -f lavfi -i anoisesrc -map 0:a -c:a pcm_s16le -ac 1 -ar 16000
> -f segment -segment_time 5 -segment_format s16le out.%03d.raw

ffmpeg -f lavfi -i
anoisesrc=sample_rate=16000:duration=35:nb_samples=16000 -map 0:a -c:a
pcm_s16le -ac 1 -f segment -segment_time 5 -segment_format s16le
out.%03d.raw

You can't resample after, you will get different number of samples per packet.


More information about the ffmpeg-user mailing list