[Python] Continuously stream audio from .m3u8 file between ffmpeg processes

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a webserver that streams audio using HLS. I'm using a python script to create an ffmpeg process that takes a random mp3 file and segments it so it can be delivered by the webserver. My issue occurs when the track ends the player stops and the page needs to be reloaded to start the next track.

    From my understanding the .m3u8 file is continuously updating with the latest segment which the browser asks for and it should be seamlessly transitioning between tracks because the segment number is incrementing properly across processes. Below is my code for starting the ffmpeg process

    next_playing_hash = track.hash
    track_hash = track.hash

    input_path = os.path.join(self.source_path, track.filepath.name)
    segment_path = os.path.join(self.output_path, "segment_%03d.ts")
    output_path = os.path.join(self.output_path, "playlist.m3u8")

    command = ["ffmpeg", "-re", "-i", input_path,
    "-map", "a", "-c:a", "aac", "-b:a", "128k", "-f", "hls",
    "-hls_time", "8", "-threads", "2","-hls_list_size", "6",
    "-hls_flags", "delete_segments+append_list+discont_start+program_date_time",
    "-hls_segment_filename", segment_path, output_path]

    logging.info(f"Started Worker for {self.name}, playing {input_path}")
    self.current_worker = subprocess.Popen(command)

    self.last_played = track

    return self.current_worker


    This is creating 8 second segments and replacing the 6th oldest segment with the newest. I suspect this could be an issue with the new process creating a new "playlist.m3u8" file however if this is the case I'm having a hard time thinking of a way around that.

    Continue reading...

Compartilhe esta Página