@@ -52,8 +52,8 @@ on the system path. For Windows, it is a bit more complicated.
5252 Alternately, the FFmpeg may be placed elsewhere and use :py:func: `ffmpegio.set_path ` to
5353 specify any arbitrary location.
5454
55- Core offerings
56- --------------
55+ Features
56+ --------
5757
5858FFmpeg can read/write virtually any multimedia file out there, and :code: `ffmpegio ` uses
5959the FFmpeg's prowess to perform media I/O (and other) operations in Python. It offers two
@@ -64,12 +64,13 @@ Media Probe
6464-----------
6565
6666To process a media file, you first need to know what's in it. Within FFmpeg
67- ecosystem, this task is handled by `ffprobe<https://ffmpeg.org/ffprobe.html> `__.
68- :code: `ffmpegio ` offers a wrapper module :ref: `ffmpegio:probe<probe> ` with 4
67+ ecosystem, this task is handled by `ffprobe <https://ffmpeg.org/ffprobe.html >`__.
68+ :code: `ffmpegio `'s :ref: `ffmpegio:probe<probe> ` module wraps ffprobe with 4
6969basic functions::
7070
7171 >>> import ffmpegio
7272 >>> from pprint import pprint
73+
7374 >>> url = ' mytestvideo.mpg'
7475 >>> format_info = ffmpegio.probe.format_basic(url)
7576 >>> pprint(format_info)
@@ -78,11 +79,13 @@ basic functions::
7879 'format_name': 'mpegts',
7980 'nb_streams': 2,
8081 'start_time': 0.0}
82+
8183 >>> stream_info = ffmpegio.probe.streams_basic(url)
8284 >>> pprint(stream_info)
8385 [{'codec_name': 'mp2', 'codec_type': 'audio', 'index': 0},
8486 {'codec_name': 'h264', 'codec_type': 'video', 'index': 1}]
85- >>> vst_info = ffmpegio.probe.video_streams_basic(url)
87+
88+ >>> vst_info = ffmpegio.probe.video_streams_basic(url)
8689 >>> pprint.pprint(vst_info)
8790 [{'codec_name': 'h264',
8891 'display_aspect_ratio': Fraction(22, 15),
@@ -94,6 +97,7 @@ basic functions::
9497 'sample_aspect_ratio': Fraction(1, 1),
9598 'start_time': 0.0,
9699 'width': 352}]
100+
97101 >>> ast_info = ffmpegio.probe.audio_streams_basic(url)
98102 >>> pprint.pprint(ast_info)
99103 [{'channel_layout': 'stereo',
@@ -106,8 +110,8 @@ basic functions::
106110 'sample_rate': 44100,
107111 'start_time': 0.0}]
108112
109- To obtain the complete ffprobe output, use :py:func: `ffmpegio.probe.inquire `. For more information,
110- see :ref: `probe `.
113+ To obtain the complete ffprobe output, use :py:func: `ffmpegio.probe.full_details `.
114+ For more information on :py:mod: ` probe `, see :ref: `probe `.
111115
112116Block Read/Write
113117----------------
@@ -183,3 +187,75 @@ write object comes with :code:`write()` method. The reader, in addition, has
183187:code: `readiter() ` generator to iterate as long as there are data to read. For more,
184188see :py:func: `ffmpegio.open `.
185189
190+ Specify Read Time Range
191+ -----------------------
192+
193+ For both block and stream read operations, you can specify the time range to read
194+ data from. There are four options available:
195+
196+ .. table :: Read Timing Options
197+ :class: tight-table
198+
199+ ======== =======================================================================
200+ Name Description
201+ ======== =======================================================================
202+ start Start time. Defaults to the beginning of the stream.
203+ end End time. Defaults to the end of the stream.
204+ duration Duration in seconds. Defaults to the duration from :code: `start ` to the
205+ end of the input stream.
206+ units Time units. One of ``seconds ``, ``frames ``, or ``samples ``. Defaults
207+ to ``seconds ``.
208+ ======== =======================================================================
209+
210+ One of :code: `start `, :code: `end `, :code: `duration ` or a combination of two of them
211+ defines the read range::
212+
213+ >>>url = "myvideo.mp4"
214+ >>>info = ffmpegio.probe.video_streams_basic(url)[0]
215+
216+ >>>#read only the first 1 seconds
217+ >>>fs, F = ffmpegio.video.read(url, duration=1.0)
218+
219+ >>>#read the last 2.5 seconds
220+ >>>fs, F = ffmpegio.video.read(url, end=info["duration"], duration=2.5)
221+
222+ >>>#read from 1.2 second mark to 2.5 second mark
223+ >>>fs, F = ffmpegio.video.read(url, start=1.2, end=2.5)
224+
225+ .. note ::
226+ If all 3 are given, the read functions honor :code: `start ` and :code: `duration `
227+ and ignore :code: `end `.
228+
229+ Rather than specifying the times and durations in seconds, :code: `units ` option
230+ allows to specify by the frame numbers for video and sample numbers for audio.
231+ For example::
232+
233+ >>>#read 30 frame from the 11th frame (remember Python uses 0-based index)
234+ >>>with ffmpegio.open("myvideo.mp4", start=10, duration=30, units='frames') as f:
235+ >>> frame = f.read()
236+ >>> # do your thing with the frame data
237+
238+ In this example, the video stream of :code: `"myvideo.mp4" ` is first probed for its
239+ frame rate, then the :code: `start ` and :code: `duration ` arguments are converted to
240+ seconds per the discovered frame rate.
241+
242+ Likewise, the timing of the audio input stream can be set with its sample number::
243+
244+ >>>#read first 10000 audio samples
245+ >>>fs, x = ffmpegio.audio.read("myaudio.wav", duration=10000, units='samples')
246+
247+ Now, you may ask about the accuracy of the timing, and this is a very important point
248+ when using FFmpeg in general. FFmpeg is a media playback/recording/transcoding
249+ tool and not a precision data analysis software. As such, it does not and cannot
250+ guarantee the time accuracy. To quote from its documentation,
251+
252+ "Note that in most formats it is not possible to seek exactly, so ffmpeg will
253+ seek to the closest seek point before position. When transcoding and ``-accurate_seek ``
254+ is enabled (the default), this extra segment between the seek point and position
255+ will be decoded and discarded."
256+
257+ This being said, video frames are generally seeked correctly with ``-accurate_seek ``.
258+ However, the audio stream timing gets a bit dicier due to its frames containing multiple
259+ samples. To overcome this :py:mod: `ffmpegio ` always reads the audio stream from the
260+ beginning and truncate unrequested samples. So, it is advised to use the stream read
261+ if multiple audio segments are needed to reduce this necessary overhead.
0 commit comments