| 1 | import asyncio |
| 2 | import logging |
| 3 | |
| 4 | import click |
| 5 | |
| 6 | from haffmpeg.camera import CameraMjpeg |
| 7 | |
| 8 | logging.basicConfig(level=logging.DEBUG) |
| 9 | |
| 10 | |
| 11 | @click.command() |
| 12 | @click.option("--ffmpeg", "-f", default="ffmpeg", help="FFmpeg binary") |
| 13 | @click.option("--source", "-s", help="Input file for ffmpeg") |
| 14 | @click.option("--output", "-o", help="Output image path") |
| 15 | @click.option("--extra", "-e", help="Extra ffmpeg command line arguments") |
| 16 | def cli(ffmpeg, source, output, extra): |
| 17 | """FFMPEG capture frame as image.""" |
| 18 | |
| 19 | async def read_stream(): |
| 20 | """Read stream inside loop.""" |
| 21 | stream = CameraMjpeg(ffmpeg_bin=ffmpeg) |
| 22 | await stream.open_camera(source, extra) |
| 23 | |
| 24 | reader = await stream.get_reader() |
| 25 | try: |
| 26 | while True: |
| 27 | data = await reader.read(2048) |
| 28 | print(data) |
| 29 | except OSError: |
| 30 | pass |
| 31 | finally: |
| 32 | await stream.close() |
| 33 | |
| 34 | asyncio.run(read_stream()) |
| 35 | |
| 36 | |
| 37 | if __name__ == "__main__": |
| 38 | cli() |