AudioClip¶
classvidiopy.AudioClip
-
Bases:
vidiopy.ClipThe AudioClip class represents an audio clip. It is a subclass of the Clip class.
- Parameters:
-
duration (int or float, optional): The duration of the audio clip. Defaults toNone.fps (int, optional): Frames per second of the audio clip. Defaults toNone.
- Attributes:
-
fps: int | None: The frames per second of the audio clip. Defaults tofpsParameter._original_dur: int | float | None: The original duration of the audio clip. Defaults todurationParameter._audio_data: np.ndarray | None: The audio data of the clip. Defaults toNone.channels: int | None: The number of audio channels. Defaults toNone._st: int | float: The start time of the audio clip. Defaults to0.0._ed: int | float | None: The end time of the audio clip. Defaults todurationParameter.
- Properties:
-
audio_data: np.ndarray-
This property gets the audio data. If the audio data is not set, it
raisesaValueError.- Returns:
np.ndarray: The audio data.- Raises:
ValueError: If the audio data is not set.
Example: :
duration: int | float-
This property gets the duration of the audio clip. The duration is represented in seconds and can be an
integer, afloat, orNoneif the duration is not set.- Note:
- You Can't Set the duration of the audio clip it is not allowed to change directly.
Raises:
AttributeError: Always raises anAttributeErrorif you try to set duration.- Returns:
int | float: The duration of the audio clip.
Example: :
start: int | float-
This property gets the start time of the audio clip. The start time is represented in seconds and can be an
integeror afloat.- Returns:
int | float: The start time of the audio clip.
Example: :
end: int | float | None-
This property gets the end time of the audio clip. The end time is represented in seconds and can be an
integer, afloat, orNoneif the end time is not set.- Returns:
int | float | None: The end time of the audio clip.
Example: :
-
- Methods:
-
defset_data(self, audio_data: np.ndarray) -> Self:-
This method sets the audio data and returns the instance of the class.
- Args:
- audio_data (np.ndarray): The audio data to set.
- Returns:
- AudioClip: The instance of the class.
Example: :
defset_fps(self, fps: int | None) -> Self:-
This method sets the frames per second (fps) for the audio clip and
returnsthe instance of theclass.- Args:
fps: int | None: The frames per second to set. IfNone, the fps will be unset.- Returns:
AudioClip:SelfThe Instance of theclass.
Example: :
defset_start(self, start: int | float) -> Self:-
This method sets the start time of the audio clip and returns the instance of the class. The start time is represented in seconds and can be an
integeror afloat.- Args:
start: int | float: The start time to set in seconds.- Returns:
AudioClip: The instance of the class with the updated start time.
Example: :
defset_end(self, end: int | float | None) -> Self:-
This method sets the end time of the audio clip and returns the instance of the class. The end time is represented in seconds and can be an
integer, afloat, orNoneif the end time is not to be set.- Args:
end: int | float | None: The end time to set in seconds.- Returns:
AudioClip: The instance of the class with the updated end time.
Example: :
defget_frame_at_t(self, t: int | float) -> np.ndarray:-
This method gets the audio frame at a specific time
t. The timetis represented in seconds and can be anintegeror afloat. It calculates the frame index using the duration, total frames, and timet, and returns the audio data at that frame index.- Args:
t: int | float: The time in seconds at which to get the audio frame.- Returns:
np.ndarray: The audio data at the specified time.- Raises:
ValueError: If frames per second (fps) is not set, audio data is not set, or original duration is not set.
defiterate_frames_at_fps(self, fps: int | float | None = None) -> Generator[np.ndarray, None, None]:-
This method generates audio frames at a specific frames per second (fps) rate. If no fps is provided, it uses the fps set in the AudioClip instance. It calculates the original fps using the duration and total frames, then generates frames at the specified fps rate.
- Args:
fps (int | float | None, optional): The frames per second rate at which to generate frames. If not provided, the fps set in the AudioClip instance is used.- Yields:
np.ndarray: The audio data at each frame.- Raises:
ValueError: If frames per second (fps) is not set, audio data is not set, or original duration is not set.
defiterate_all_frames(self) -> Generator[np.ndarray, None, None]:-
This method generates all audio frames in the
AudioClipinstance. It iterates over each frame in the audio data andyieldsit.- Yields:
np.ndarray: The audio data at each frame.- Raises:
ValueError: If audio data is not set.
deffl_frame_transform(self, func, *args, **kwargs) -> Self:-
This method applies a function to each frame of the audio data. The function should take a frame (an ndarray of channel data) as its first argument, followed by any number of additional positional and keyword arguments.
- Args:
-
func (Callable): The function to apply to each frame. It should take a frame (an ndarray of channel data) as its first argument.*args: Additional positional arguments to pass to the function.**kwargs: Additional keyword arguments to pass to the function.
- Returns:
AudioClip: The instance of the class with the transformed audio data.- Raises:
ValueError: If audio data is not set.
deffl_clip_transform(self, func, *args, **kwargs) -> Self:-
This method applies a function to the entire audio data. The function should take the AudioClip instance as its first argument, followed by any number of additional positional and keyword arguments.
- Args:
-
func (Callable): The function to apply to the audio data. It should take the AudioClip instance as its first argument.*args: Additional positional arguments to pass to the function.**kwargs: Additional keyword arguments to pass to the function.
- Returns:
AudioClip: The instance of the class with the transformed audio data.- Raises:
ValueError: If audio data is not set.
deffl_time_transform(self, func: Callable[[int | float], int | float]) -> Self:-
This method applies a time transformation function to the
get_frame_at_tmethod of the AudioClip instance. The transformation function should take a time (an integer or a float) as its argument and return a transformed time.The
get_frame_at_tmethod is replaced with a new method that applies the transformation function to its argument before calling the original method.- Args:
func (Callable[[int | float], int | float]): The time transformation function to apply. It should take a time (an integer or a float) as its argument and return a transformed time.- Returns:
AudioClip: The instance of the class with the transformedget_frame_at_tmethod.- Raises:
ValueError: If theget_frame_at_tmethod is not set.
defsub_clip_copy(self, start: float | int | None = None, end: float | int | None = None) -> Self-
This method creates a copy of the AudioClip instance and then creates a subclip from the audio clip starting from
starttoendin the copied instance. Ifstartorendis not provided, it uses the start or end time set in the AudioClip instance. If neither is set, it uses 0 for start and the duration for end.It calculates the original frames per second (fps) using the duration and total frames, then calculates the start and end frame indices using the original fps. It then updates the audio data, original duration, end time, and start time of the copied AudioClip instance.
- Args:
-
start (float | int | None, optional): The start time of the subclip in seconds. If not provided, the start time set in the AudioClip instance is used. Defaults toNone.end (float | int | None, optional): The end time of the subclip in seconds. If not provided, the end time set in the AudioClip instance is used. Defaults toNone.
- Returns:
AudioClip: A copy of the instance of the class with the updated audio data, original duration, end time, and start time.- Raises:
ValueError: If audio data is not set, original duration is not set, or end time is greater than the original duration.
defcopy(self) -> Self:- This method creates a deep copy of the AudioClip instance and returns it. It uses the
copy_function, which should be a deep copy function likecopy.deepcopyin Python's standard library. - Returns:
AudioClip: A deep copy of the instance of the class.- Raises:
ValueError: If thecopy_function is not set or does not correctly create a deep copy.- >
defwrite_audiofile(self, path: str, fps: int | None = None, overwrite=True, show_log=False, **kwargs) -> None: -
This method writes the audio data to an audio file at the specified path. It uses the frames per second (fps) if provided, otherwise it uses the fps set in the
AudioClipinstance. It raises aValueErrorif fps is not set in either way. It also raises aValueErrorif audio data, original duration, or channels are not set.It creates a temporary audio data array by getting the frame at each time step from
0to the end or duration with a step of1/fps. It then writes the temporary audio data to the audio file using theffmpegio.audio.writefunction.- Args:
-
- path (str): The path to write the audio file to.
- fps (int | None, optional): The frames per second to use. If not provided, the fps set in the AudioClip instance is used. Defaults to None.
- overwrite (bool, optional): Whether to overwrite the audio file if it already exists. Defaults to True.
- show_log (bool, optional): Whether to show the log of the
ffmpegio.audio.writefunction. Defaults to False. - **kwargs: Additional keyword arguments to pass to the
ffmpegio.audio.writefunction.
- Raises:
ValueError: If fps is not set, audio data is not set, original duration is not set, or channels are not set.
-