pythonlinuxpython-3.xoggvorbis

Python: how to write binary data to the stdout so that bash script can use process substitution?


I need to use the program called ogg123 for converting input ogg file to wav. My ogg is on S3 - so I need to download it first and transcode after that. But I think that will be faster to transcode sound on the fly - without writing to the disk source ogg file - using process substitution.

From bash I want to do this:

ogg123 -d wav <(./test.py) -f out.wav

In Python, for binary output, I tried:

os.write

sys.stdout.buffer.write

fp = os.fdopen(sys.stdout.fileno(), 'wb'); fp.write

For example:

#!/usr/bin/env python3

import os

import boto3

s3_client = boto3.client('s3')

os.write(1, 
    s3_client.get_object(
        Bucket='my-bucket-name',
        Key='file.ogg'
    )['Body'].read()
)

./test.py in all cases print visually valid data. For example:

xxd <(./test.py) | head print:

00000000: 4f67 6753 0002 0000 0000 0000 0000 0000  OggS............
00000010: 0000 0000 0000 5664 269e 011e 0176 6f72  ......Vd&....vor
00000020: 6269 7300 0000 0001 2256 0000 0000 0000  bis....."V......
00000030: 058a 0000 0000 0000 a901 4f67 6753 0000  ..........OggS..
00000040: 0000 0000 0000 0000 0000 0000 0100 0000  ................
00000050: 5096 ab47 0e16 ffff ffff ffff ffff ffff  P..G............
00000060: ffff c503 766f 7262 6973 0600 0000 6666  ....vorbis....ff
00000070: 6d70 6567 0000 0000 0105 766f 7262 6973  mpeg......vorbis
00000080: 2242 4356 0100 4000 0018 4210 2a05 ad63  "BCV..@...B.*..c
00000090: 8e3a c815 218c 19a2 a042 ca29 c71d 42d0  .:..!....B.)..B.

But all these variants generates the same error from ogg123:

Error opening /dev/fd/63 using the oggvorbis module. The file may be corrupted.

I tried the same file locally - file is correct:

$ ogg123 -d wav file.ogg -f out.wav

Audio Device:   WAV file output

Playing: file.ogg
Ogg Vorbis stream: 1 channel, 22050 Hz

Done.

What can you recommend?


Solution

  • Thank you for help, I found that I can fetch through S3 using pure ogg123:

    ogg123 -d wav https://s3.amazonaws.com/mybucket/file.ogg -f out.wav