Individual audio video reader with convenient indexing function. Parameters ---------- uri: str Path of file. ctx: decord.Context The context to decode the file, can be decord.cpu() or decord.gpu(). sample_rate: int, default is -1 Desired output sample ra
| 12 | from .bridge import bridge_out |
| 13 | |
| 14 | class AVReader(object): |
| 15 | """Individual audio video reader with convenient indexing function. |
| 16 | |
| 17 | Parameters |
| 18 | ---------- |
| 19 | uri: str |
| 20 | Path of file. |
| 21 | ctx: decord.Context |
| 22 | The context to decode the file, can be decord.cpu() or decord.gpu(). |
| 23 | sample_rate: int, default is -1 |
| 24 | Desired output sample rate of the audio, unchanged if `-1` is specified. |
| 25 | mono: bool, default is True |
| 26 | Desired output channel layout of the audio. `True` is mono layout. `False` is unchanged. |
| 27 | width : int, default is -1 |
| 28 | Desired output width of the video, unchanged if `-1` is specified. |
| 29 | height : int, default is -1 |
| 30 | Desired output height of the video, unchanged if `-1` is specified. |
| 31 | num_threads : int, default is 0 |
| 32 | Number of decoding thread, auto if `0` is specified. |
| 33 | fault_tol : int, default is -1 |
| 34 | The threshold of corupted and recovered frames. This is to prevent silent fault |
| 35 | tolerance when for example 50% frames of a video cannot be decoded and duplicate |
| 36 | frames are returned. You may find the fault tolerant feature sweet in many cases, |
| 37 | but not for training models. Say `N = # recovered frames` |
| 38 | If `fault_tol` < 0, nothing will happen. |
| 39 | If 0 < `fault_tol` < 1.0, if N > `fault_tol * len(video)`, raise `DECORDLimitReachedError`. |
| 40 | If 1 < `fault_tol`, if N > `fault_tol`, raise `DECORDLimitReachedError`. |
| 41 | |
| 42 | """ |
| 43 | |
| 44 | def __init__(self, uri, ctx=cpu(0), sample_rate=44100, mono=True, width=-1, height=-1, num_threads=0, fault_tol=-1): |
| 45 | self.__audio_reader = AudioReader(uri, ctx, sample_rate, mono) |
| 46 | self.__audio_reader.add_padding() |
| 47 | if hasattr(uri, 'read'): |
| 48 | uri.seek(0) |
| 49 | self.__video_reader = VideoReader(uri, ctx, width, height, num_threads, fault_tol) |
| 50 | |
| 51 | def __len__(self): |
| 52 | """Get length of the video. Note that sometimes FFMPEG reports inaccurate number of frames, |
| 53 | we always follow what FFMPEG reports. |
| 54 | Returns |
| 55 | ------- |
| 56 | int |
| 57 | The number of frames in the video file. |
| 58 | """ |
| 59 | return len(self.__video_reader) |
| 60 | |
| 61 | def __getitem__(self, idx): |
| 62 | """Get audio samples and video frame at `idx`. |
| 63 | |
| 64 | Parameters |
| 65 | ---------- |
| 66 | idx : int or slice |
| 67 | The frame index, can be negative which means it will index backwards, |
| 68 | or slice of frame indices. |
| 69 | |
| 70 | Returns |
| 71 | ------- |
no outgoing calls