Added proper script

This commit is contained in:
Fabio Manganiello 2020-11-02 16:17:57 +01:00
parent d1261a6a05
commit 9f15010346
3 changed files with 111 additions and 3 deletions

View File

@ -1,6 +1,6 @@
# micstream
Stream an audio input device over HTTP as mp3
Stream an audio input device over HTTP as mp3.
## Requirements
@ -10,3 +10,42 @@ Stream an audio input device over HTTP as mp3
[sudo] apt-get install ffmpeg lame
```
## Installation
```bash
[sudo] python setup.py install
```
## Usage
```bash
micstream --help <<<
usage: micstream [-h] -d DEVICE [-s AUDIO_SYSTEM] [-v] [-a ADDRESS] [-p PORT] [-e ENDPOINT] [-r SAMPLE_RATE] [-b BITRATE]
[-c CHANNELS] [-f FFMPEG_BIN] [-B BUFSIZE]
Stream an audio source over HTTP as mp3
optional arguments:
-h, --help show this help message and exit
-d DEVICE, --device DEVICE
ALSA/Pulse device ID/name
-s AUDIO_SYSTEM, --sound-system AUDIO_SYSTEM
Sound system. Supported: alsa, pulse. Default: alsa
-v, --verbose Verbose/debug mode
-a ADDRESS, --address ADDRESS
Bind address (default: 0.0.0.0)
-p PORT, --port PORT HTTP listen port (default: 8080)
-e ENDPOINT, --endpoint ENDPOINT
HTTP endpoint for streaming (default: /stream.mp3)
-r SAMPLE_RATE, --sample-rate SAMPLE_RATE
Recording sample rate (default: 44100)
-b BITRATE, --bitrate BITRATE
mp3 compression bitrate, in kbps (default: 128)
-c CHANNELS, --channels CHANNELS
Number of recording channels (default: 1)
-f FFMPEG_BIN, --ffmpeg FFMPEG_BIN
Path to the FFmpeg binary (default: ffmpeg)
-B BUFSIZE, --bufsize BUFSIZE
Size of the audio chunks to be delivered to the server (default: 8192 bytes)
```

View File

@ -1,3 +1,4 @@
import argparse
import logging
import sys
@ -11,11 +12,39 @@ def init_logging():
datefmt='%Y-%m-%d %H:%M:%S')
def get_args():
parser = argparse.ArgumentParser(description='Stream an audio source over HTTP as mp3')
parser.add_argument('-d', '--device', help='ALSA/Pulse device ID/name', required=True, dest='device')
parser.add_argument('-s', '--sound-system', help='Sound system. Supported: alsa, pulse. Default: alsa', required=False, default='alsa', dest='audio_system')
parser.add_argument('-v', '--verbose', help='Verbose/debug mode', required=False, action='store_true', dest='debug')
parser.add_argument('-a', '--address', help='Bind address (default: 0.0.0.0)', required=False, default='0.0.0.0', dest='address')
parser.add_argument('-p', '--port', help='HTTP listen port (default: 8080)', required=False, default=8080, type=int, dest='port')
parser.add_argument('-e', '--endpoint', help='HTTP endpoint for streaming (default: /stream.mp3)', required=False, default='/stream.mp3', dest='endpoint')
parser.add_argument('-r', '--sample-rate', help='Recording sample rate (default: 44100)', required=False, default=44100, type=int, dest='sample_rate')
parser.add_argument('-b', '--bitrate', help='mp3 compression bitrate, in kbps (default: 128)', required=False, default=128, type=int, dest='bitrate')
parser.add_argument('-c', '--channels', help='Number of recording channels (default: 1)', required=False, default=1, type=int, dest='channels')
parser.add_argument('-f', '--ffmpeg', help='Path to the FFmpeg binary (default: ffmpeg)', required=False, default='ffmpeg', dest='ffmpeg_bin')
parser.add_argument('-B', '--bufsize', help='Size of the audio chunks to be delivered to the server (default: 8192 bytes)', required=False, default=8192, type=int, dest='bufsize')
opts, _ = parser.parse_known_args(sys.argv[1:])
return opts
def main():
init_logging()
args = get_args()
with AudioSource('plughw:3,0') as source, \
Server() as server:
with AudioSource(device=args.device,
audio_system=args.audio_system,
sample_rate=args.sample_rate,
bitrate=args.bitrate,
channels=args.channels,
ffmpeg_bin=args.ffmpeg_bin,
bufsize=args.bufsize,
debug=args.debug) as source, \
Server(host=args.address,
port=args.port,
endpoint=args.endpoint,
debug=args.debug) as server:
for sample in source:
server.process_audio(sample)

40
setup.py Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python
import os
from setuptools import setup, find_packages
def path(fname=''):
return os.path.abspath(os.path.join(os.path.dirname(__file__), fname))
def readfile(fname):
with open(path(fname)) as f:
return f.read()
setup(
name="micstream",
version="0.1",
author="Fabio Manganiello",
author_email="info@fabiomanganiello.com",
description="Stream an audio source as mp3 over HTTP",
license="MIT",
python_requires='>= 3.6',
keywords="mp3 stream http",
url="https://github.com/BlackLight/micstream",
packages=find_packages(),
include_package_data=True,
long_description=readfile('README.md'),
long_description_content_type='text/markdown',
entry_points={
'console_scripts': [
'micstream=micstream.__main__:main',
],
},
classifiers=[
"Topic :: Utilities",
"License :: OSI Approved :: MIT License",
"Development Status :: 3 - Alpha",
],
)