dynamic generate m3u8 list with EXT-X-BYTERANGE - http-live-streaming

I want to fetch hls video segments by time interval.
Suppose I have seg1.m4s / seg2.m4s ..., and each seg is 1 minutes long and size is 10000 bytes.
If I want to play from 1:30 ~ 3:30, it means the hls would start from middle of seg2 to middle of seg4.
This why I would need to dynamic generate m3u8 list and EXT-X-BYTERANGE.
However I encounter some problem.
Here is my m3u8 list now
#EXTM3U
#EXT-X-VERSION:7
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-ALLOW-CACHE:YES
#EXT-X-MAP:URI="init.mp4",
#EXTINF:30.0,
#EXT-X-BYTERANGE:5000#5000,
seg2.m4s
#EXTINF:60.0,
seg3.m4s
#EXTINF:30.0,
#EXT-X-BYTERANGE:5000#0,
seg4.m4s
#EXT-X-ENDLIST
It would play only seg2.m4s and stop.

Related

Find a specified audio pattern in a collection of audio files

I have a collection of 1000 audio files. Those files are music files stored in MP3 format, some are 10 minutes long, some are 15 seconds long.
I also have one audio file that is 1 second long, also in MP3. I would like to find which files from my collection contain this 1-second audio file. This 1-second audio file can have a slightly worse audio quality than my collection, because it was recoded, recompressed, etc, so I would need some more intelligent comparison method than simple binary search through my collection of files.
What should I read about if I'd like to create a small program for searching through my collection of audio files for this 1-second audio pattern?
Edit: In other words: Each file in my collection of 1000 files is a song. This 1-second fragment I'm searching for is a fragment of some song from this collection, and I'd like to know what song it's taken from.
Here's one way to do it using len(audioSegment) feaure of pydub.
"../so/*.mp3" holds your audio files. Or you can iterate through various folders using glob.
from glob import glob
from pydub import AudioSegment
for mp3_file in glob("../so/*.mp3"):
tmpAudioSegment = AudioSegment.from_mp3(mp3_file)
#Note: pydub len returns in milliseconds.
print("{} : {}".format(mp3_file,len(tmpAudioSegment)/1000))
Result
../so/Bubbles.mp3 : 10 sec
../so/Drone Dark Suspense 2.mp3 : 30 sec
../so/Sci-Fi Sweep.mp3 : 8 sec
../so/Pinball Machine.mp3 : 22 sec
../so/Title Flange Sweep Hit.mp3 : 3 sec
../so/Whale Sounds.mp3 : 6 sec
../so/Ambient Hit.mp3 : 2 sec
../so/Golf Hit 3.mp3 : 1 sec

Creating M4A file from MP4

I want to create an M4A file from an MP4, I want to attempt this from scratch without using other libraries but just the raw data.
So far I am able to locate the atom moov and parser it. And as a result I can pull the audio data from the mdat. So then I create my own M4A file with the right ftyp (M4A isomiso2) then add a new mdat with just the audio data I previously recovered, finally I add the moov with the same mvhd, and only the audio trak but with an updated stco to reflect the change in offsets of the chunks of audio data (as they are just one after each other now). I am sure I am doing all of this right.
However the M4A file just plays silence. I believe it is because I have to edit more in the moov but I am not sure what - I put it into FFmpeg corruption and I got:
"Sample rate index in program config element does not match the sample rate index configured by the container."
"Too large remapped id is not implemented."
So as a result I think it is something to do with the stsd atom but I am not sure how to change it.

in a Variant live m3u8 file, does .ts file have the same sequence number in different bitrate playlist with video content

the question is:
in different level playlist who have the same sequence number, MUST they present the same content(e.g. both level1-fileSequence2.ts, level2-fileSequence2.ts duration is 10:00:00-10:00:10) ?
OR for thest different level's ts file, if they present the same period content, must they have the same sequence number?
playlist
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=150000,RESOLUTION=416x234, \
CODECS="avc1.42e00a,mp4a.40.2"
level1.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=240000,RESOLUTION=416x234, \
CODECS="avc1.42e00a,mp4a.40.2"
level2.m3u8
level1.m3u8
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:1
#EXTINF:10,
level1-fileSequence1.ts
#EXTINF:10,
level1-fileSequence2.ts
level2.m3u8
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:2
#EXTINF:10,
level2-fileSequence2.ts
#EXTINF:10,
level2-fileSequence3.ts
Not necessarily, for both questions.
As a quick note, the name of the file doesn't matter at all. From the HLS specification:
A client MUST NOT assume that segments with the same Media Sequence
Number in different Variant Streams or Renditions have the same
position in the presentation; Playlists MAY have independent Media
Sequence Numbers. Instead, a client MUST use the relative position
of each segment on the Playlist timeline and its Discontinuity
Sequence Number to locate corresponding segments.
Source: HLS Draft

Quicktime playing unfinished HLS event playlist can't seek

I have an HLS playlist of type EVENT. It has the #EXT-X-PLAYLIST-TYPE:EVENT tag. The playlist is not finished. It does not have an #EXT-X-ENDLIST tag.
When I play the playlist in Quicktime, it doesn't allow me to seek around. Since this is an event playlist, all the previous segments are available. Quicktime should have everything it needs to seek back.
I have tried this on OS X 10.6 and Maverick and the problem occurs. I have also tried this on an old iPad running iOS 5. On iOS 5 the problem doesn't occur.
Here is the playlist itself:
#EXTM3U
#EXT-X-VERSION:6
#EXT-X-ALLOW-CACHE:NO
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-START:TIME-OFFSET=0,PRECISE=NO
#EXT-X-TARGETDURATION:10
#EXTINF:10,
live_robocop_hd-rate_01_6512.ts
#EXTINF:10,
live_robocop_hd-rate_01_6513.ts
#EXTINF:10,
live_robocop_hd-rate_01_6514.ts
#EXTINF:10,
live_robocop_hd-rate_01_6515.ts
# ... more segments here ...

How to convert gapless m4a tracks into a gapless HLS stream

I have an album stored as a list of gapless m4a files, ripped from CD. I need to stream the album gaplessly over HTTP Live Streaming, and the user must be able to "jump in" at the start of any track. For now, my only client is AVPlayer on iOS.
I can segment the tracks individually using Apple's mediafilesegmenter tool. For each track, this produces one .m3u8 playlist file and several .aac segment files, each ~10 seconds in duration except for the last.
The m3u8 playlist for Track 1 looks like this:
#EXTM3U
#EXT-X-TARGETDURATION:11
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:10.001,
segment0.aac
#EXTINF:9.983,
segment1.aac
...
#EXTINF:3.231,
segment23.aac
#EXT-X-ENDLIST
I can combine these m3u8 playlist files into one master m3u8 file for the album:
#EXTM3U
#EXT-X-TARGETDURATION:11
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:10.001, // begin track 1
segment0.aac
#EXTINF:9.983,
segment1.aac
...
#EXTINF:3.231,
segment23.aac
#EXT-X-DISCONTINUITY
#EXTINF:10.001, // begin track 2
segment24.aac
#EXTINF:9.983,
segment25.aac
...
#EXTINF:6.845,
segment46.aac
#EXT-X-DISCONTINUITY
#EXTINF:10.001, // begin track 3
segment47.aac
#EXTINF:9.983,
segment48.aac
...
#EXTINF:8.012,
segment80.aac
#EXT-X-ENDLIST
It will play through the whole album, but it isn't gapless. Notice the DISCONTINUITY tag between each track (without it, the player hangs forever). This introduces a small gap between tracks, maybe 300 milliseconds.
How can I create segments that flow into each other with no discontinuity?
You can concatenate the AAC files before using the mediafilesegmenter tool on the combined AAC file.
The following ffmpeg command might generate the output file.
ffmpeg -i "concat:input1.aac|input2.aac|input3.aac" -c copy output.aac
It's possible you'll need to remux the aac files to mpeg2ts files before concatenation, and then remux the mpeg2ts file to AAC.

Resources