51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
from streamer import Streamer, is_alive
|
|
import subprocess
|
|
import time
|
|
import sys
|
|
import os
|
|
|
|
class FileRadio(Streamer):
|
|
PORT = 8554
|
|
|
|
def __init__(self, path):
|
|
self.path = path
|
|
self.basename = os.path.basename(self.path)
|
|
self.name, self.ext = os.path.splitext(self.basename)
|
|
|
|
super().__init__()
|
|
|
|
def _stream_path(self):
|
|
return f":{FileRadio.PORT}/sample/{self.name}"
|
|
|
|
def _stream_thread(self):
|
|
self.playback = subprocess.Popen(
|
|
[
|
|
'/usr/bin/ffmpeg', '-re', '-stream_loop', '-1', '-i',
|
|
f"{self.path}", '-c', 'copy',
|
|
'-f', 'rtsp', f"rtsp://localhost{self._stream_path()}"
|
|
],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL
|
|
)
|
|
|
|
while self.run:
|
|
if not is_alive(self.playback):
|
|
print('Playback failed, aborting stream.', file=sys.stderr)
|
|
break
|
|
time.sleep(0.1)
|
|
|
|
self.run = False
|
|
|
|
self.playback.kill()
|
|
self.playback.wait()
|
|
self.playback = None
|
|
|
|
|
|
if __name__ == '__main__':
|
|
fr = FileRadio('./data/sampleaudio/taunt.mp3')
|
|
fr.start_stream()
|
|
|
|
while True:
|
|
time.sleep(1)
|