1- import sys
21import numpy as np
3- from . import ffmpeg , probe , utils
2+ from . import ffmpeg , utils , configure
43
54
6-
7-
8- def read (filename , ** inopts ):
5+ def read (input_url , stream_id = 0 , ** options ):
96 """Open an audio file.
107
118 :param filename: Input media file.
@@ -14,49 +11,46 @@ def read(filename, **inopts):
1411 :return: sample rate and audio data matrix (column=time,row=channel)
1512 :rtype: (float, numpy.ndarray)
1613 """
17- info = probe .audio_streams_basic (
18- filename , index = 0 , entries = ("sample_rate" , "sample_fmt" , "channels" )
19- )[0 ]
2014
21- acodec , dtype = utils . get_audio_format ( info [ "sample_fmt" ] )
15+ args = configure . input_timing ( input_url , astream_id = stream_id , ** options )
2216
23- args = dict (
24- inputs = [(filename , None ,)],
25- outputs = [("-" , dict (vn = None , acodec = acodec , f = "rawvideo" , map = "a:0" ))],
17+ args , reader_cfg = configure .audio_io (
18+ input_url ,
19+ stream_id ,
20+ output_url = "-" ,
21+ format = "rawvideo" ,
22+ ffmpeg_args = args ,
23+ ** options ,
2624 )
2725
26+ dtype , nch , rate = reader_cfg [0 ]
2827 stdout = ffmpeg .run_sync (args )
29- return (
30- info ["sample_rate" ],
31- np .frombuffer (stdout , dtype = dtype ).reshape (- 1 , info ["channels" ]),
32- )
28+ return rate , np .frombuffer (stdout , dtype = dtype ).reshape (- 1 , nch )
3329
3430
35- def write (filename , rate , data , ** outopts ):
31+ def write (url , rate , data , ** options ):
3632 """Write a NumPy array as an audio file.
3733
38- :param filename : Output media file.
39- :type filename : str
34+ :param url : Output media file.
35+ :type url : str
4036 :param rate: The sample rate (in samples/sec).
4137 :type rate: int
4238 :param data: A 1-D or 2-D NumPy array of either integer or float data-type.
4339 :type data: numpy.ndarray
4440 :raises Exception: FFmpeg error
4541 """
46- acodec , _ = utils .get_audio_format (data .dtype )
47- args = dict (
48- inputs = [
49- (
50- "-" ,
51- dict (
52- vn = None ,
53- f = acodec [4 :],
54- ar = rate ,
55- channels = data .shape [1 ] if data .ndim > 1 else 1 ,
56- ),
57- )
58- ],
59- outputs = [(filename , None ,)],
42+ args = configure .input_timing (
43+ "-" ,
44+ astream_id = 0 ,
45+ excludes = ("start" , "end" , "duration" ),
46+ ** {"input_sample_rate" : rate , ** options },
47+ )
48+
49+ configure .audio_io (
50+ utils .array_to_audio_input (data , format = True ),
51+ output_url = url ,
52+ ffmpeg_args = args ,
53+ ** options ,
6054 )
6155
6256 ffmpeg .run_sync (args , input = data .tobytes ())
0 commit comments