AudioClip¶
class
vidiopy.AudioClip
-
Bases:
vidiopy.Clip
The 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 tofps
Parameter._original_dur: int | float | None
: The original duration of the audio clip. Defaults toduration
Parameter._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 toduration
Parameter.
- Properties:
-
audio_data: np.ndarray
-
This property gets the audio data. If the audio data is not set, it
raises
aValueError
.- 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
, orNone
if 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 anAttributeError
if 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
integer
or 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
, orNone
if the end time is not set.- Returns:
int | float | None
: The end time of the audio clip.
Example: :
-
- Methods:
-
def
set_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: :
def
set_fps(self, fps: int | None) -> Self
:-
This method sets the frames per second (fps) for the audio clip and
returns
the instance of theclass
.- Args:
fps: int | None
: The frames per second to set. IfNone
, the fps will be unset.- Returns:
AudioClip
:Self
The Instance of theclass
.
Example: :
def
set_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
integer
or 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: :
def
set_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
, orNone
if 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: :
def
get_frame_at_t(self, t: int | float) -> np.ndarray
:-
This method gets the audio frame at a specific time
t
. The timet
is represented in seconds and can be aninteger
or 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.
def
iterate_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.
def
iterate_all_frames(self) -> Generator[np.ndarray, None, None]:
-
This method generates all audio frames in the
AudioClip
instance. It iterates over each frame in the audio data andyields
it.- Yields:
np.ndarray
: The audio data at each frame.- Raises:
ValueError
: If audio data is not set.
def
fl_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.
def
fl_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.
def
fl_time_transform(self, func: Callable[[int | float], int | float]) -> Self:
-
This method applies a time transformation function to the
get_frame_at_t
method 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_t
method 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_t
method.- Raises:
ValueError
: If theget_frame_at_t
method is not set.
def
sub_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
start
toend
in the copied instance. Ifstart
orend
is 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.
def
copy(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.deepcopy
in 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.- >
def
write_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
AudioClip
instance. It raises aValueError
if fps is not set in either way. It also raises aValueError
if 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
0
to the end or duration with a step of1/fps
. It then writes the temporary audio data to the audio file using theffmpegio.audio.write
function.- 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.write
function. Defaults to False. - **kwargs: Additional keyword arguments to pass to the
ffmpegio.audio.write
function.
- Raises:
ValueError
: If fps is not set, audio data is not set, original duration is not set, or channels are not set.
-