Utils
Auto-generated documentation for musicalgestures._utils module.
- Mgt-python / Modules / Musicalgestures / Utils
- FFmpegError
- FFprobeError
- MgFigure
- MgImage
- MgProgressbar
- NoDurationError
- NoStreamError
- WrongContainer
- audio_dilate
- cast_into_avi
- clamp
- convert
- convert_to_avi
- convert_to_grayscale
- convert_to_mp4
- convert_to_webm
- crop_ffmpeg
- embed_audio_in_video
- extract_subclip
- extract_wav
- ffmpeg_cmd
- ffprobe
- frame2ms
- framediff_ffmpeg
- generate_outfilename
- get_box_video_ratio
- get_first_frame_as_image
- get_fps
- get_frame_planecount
- get_framecount
- get_length
- get_widthheight
- has_audio
- in_colab
- motiongrams_ffmpeg
- motionvideo_ffmpeg
- pass_if_container_is
- pass_if_containers_match
- quality_metrics
- rotate_video
- roundup
- scale_array
- scale_num
- str2sec
- threshold_ffmpeg
- unwrap_str
- wrap_str
FFmpegError
class FFmpegError(Exception):
def __init__(message):
FFprobeError
class FFprobeError(Exception):
def __init__(message):
MgFigure
class MgFigure():
def __init__(
figure=None,
figure_type=None,
data=None,
layers=None,
image=None,
):
Class for working with figures and plots within the Musical Gestures Toolbox.
MgFigure().show
def show():
Shows the internal matplotlib.pyplot.figure.
MgImage
class MgImage():
def __init__(filename):
Class for handling images in the Musical Gestures Toolbox.
MgProgressbar
class MgProgressbar():
def __init__(
total=100,
time_limit=0.5,
prefix='Progress',
suffix='Complete',
decimals=1,
length=40,
fill='█',
):
Calls in a loop to create terminal progress bar.
MgProgressbar().adjust_printlength
def adjust_printlength():
MgProgressbar().get_now
def get_now():
Gets the current time.
Returns
datetime.datetime.timestamp- The current time.
MgProgressbar().over_time_limit
def over_time_limit():
Checks if we should redraw the progress bar at this moment.
Returns
bool- True if equal or more time has passed thanself.time_limitsince the last redraw.
MgProgressbar().progress
def progress(iteration):
Progresses the progress bar to the next step.
Arguments
iterationfloat - The current iteration. For example, the 57th out of 100 steps, or 12.3s out of the total 60s.
NoDurationError
class NoDurationError(FFprobeError):
See also
NoStreamError
class NoStreamError(FFprobeError):
See also
WrongContainer
class WrongContainer(Exception):
def __init__(message):
audio_dilate
def audio_dilate(
filename,
dilation_ratio=1,
target_name=None,
overwrite=False,
):
Time-stretches or -shrinks (dilates) an audio file using ffmpeg.
Arguments
filenamestr - Path to the audio file to dilate.dilation_ratiofloat, optional - The source file's length divided by the resulting file's length. Defaults to 1.target_namestr, optional - The name of the output video. Defaults to None (which assumes that the input filename with the suffix "_dilated" should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the output audio file.
cast_into_avi
def cast_into_avi(filename, target_name=None, overwrite=False):
Experimental Casts a video into and .avi container using ffmpeg. Much faster than convert_to_avi, but does not always work well with cv2 or built-in video players.
Arguments
filenamestr - Path to the input video file.target_namestr, optional - Target filename as path. Defaults to None (which assumes that the input filename should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the output '.avi' file.
clamp
def clamp(num, min_value, max_value):
Clamps a number between a minimum and maximum value.
Arguments
numfloat - The number to clamp.min_valuefloat - The minimum allowed value.max_valuefloat - The maximum allowed value.
Returns
float- The clamped number.
convert
def convert(filename, target_name, overwrite=False):
Converts a video to another format/container using ffmpeg.
Arguments
filenamestr - Path to the input video file to convert.target_namestr - Target filename as path.overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the output file.
convert_to_avi
def convert_to_avi(filename, target_name=None, overwrite=False):
Converts a video to one with .avi extension using ffmpeg.
Arguments
filenamestr - Path to the input video file to convert.target_namestr, optional - Target filename as path. Defaults to None (which assumes that the input filename should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the output '.avi' file.
convert_to_grayscale
def convert_to_grayscale(filename, target_name=None, overwrite=False):
Converts a video to grayscale using ffmpeg.
Arguments
filenamestr - Path to the input video file.target_namestr, optional - Target filename as path. Defaults to None (which assumes that the input filename with the suffix "_gray" should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the grayscale video file.
convert_to_mp4
def convert_to_mp4(filename, target_name=None, overwrite=False):
Converts a video to one with .mp4 extension using ffmpeg.
Arguments
filenamestr - Path to the input video file to convert.target_namestr, optional - Target filename as path. Defaults to None (which assumes that the input filename should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the output '.mp4' file.
convert_to_webm
def convert_to_webm(filename, target_name=None, overwrite=False):
Converts a video to one with .webm extension using ffmpeg.
Arguments
filenamestr - Path to the input video file to convert.target_namestr, optional - Target filename as path. Defaults to None (which assumes that the input filename should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the output '.webm' file.
crop_ffmpeg
def crop_ffmpeg(filename, w, h, x, y, target_name=None, overwrite=False):
Crops a video using ffmpeg.
Arguments
filenamestr - Path to the input video file.wint - The desired width.hint - The desired height.xint - The horizontal coordinate of the top left pixel of the cropping rectangle.yint - The vertical coordinate of the top left pixel of the cropping rectangle.target_namestr, optional - The name of the output video. Defaults to None (which assumes that the input filename with the suffix "_crop" should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to False.
Returns
str- Path to the output video.
embed_audio_in_video
def embed_audio_in_video(source_audio, destination_video, dilation_ratio=1):
Embeds an audio file as the audio channel of a video file using ffmpeg.
Arguments
source_audiostr - Path to the audio file to embed.destination_videostr - Path to the video file to embed the audio file in.dilation_ratiofloat, optional - The source file's length divided by the resulting file's length. Defaults to 1.
extract_subclip
def extract_subclip(filename, t1, t2, target_name=None, overwrite=False):
Extracts a section of the video using ffmpeg.
Arguments
filenamestr - Path to the input video file.t1float - The start of the section to extract in seconds.t2float - The end of the section to extract in seconds.target_namestr, optional - The name for the output file. If None, the name will be \<input name>SUB\<start time in ms>_\<end time in ms>.\<file extension>. Defaults to None.overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- Path to the extracted section as a video.
extract_wav
def extract_wav(filename, target_name=None, overwrite=False):
Extracts audio from video into a .wav file via ffmpeg.
Arguments
filenamestr - Path to the video file from which the audio track shall be extracted.target_namestr, optional - The name of the output video. Defaults to None (which assumes that the input filename should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the output audio file.
ffmpeg_cmd
def ffmpeg_cmd(
command,
total_time,
pb_prefix='Progress',
print_cmd=False,
stream=True,
pipe=None,
):
Run an ffmpeg command in a subprocess and show progress using an MgProgressbar.
Arguments
commandlist - The ffmpeg command to execute as a list. Eg. ['ffmpeg', '-y', '-i', 'myVid.mp4', 'myVid.mov']total_timefloat - The length of the output. Needed mainly for the progress bar.pb_prefixstr, optional - The prefix for the progress bar. Defaults to 'Progress'.print_cmdbool, optional - Whether to print the full ffmpeg command to the console before executing it. Good for debugging. Defaults to False.streambool, optional - Whether to have a continuous output stream or just (the last) one. Defaults to True (continuous stream).pipestr, optional - Whether to pipe video frames from FFmpeg to numpy array. Possible to read the video frame by frame with pipe='read', to load video in memory with pipe='load', or to write the frames of a numpy array to a video file with pipe='write'. Defaults to None.
Raises
KeyboardInterrupt- If the user stops the process.FFmpegError- If the ffmpeg process was unsuccessful.
ffprobe
def ffprobe(filename):
Returns info about video/audio file using FFprobe.
Arguments
filenamestr - Path to the video file to measure.
Returns
str- decoded FFprobe output (stdout) as one string.
frame2ms
def frame2ms(frame, fps):
Converts frames to milliseconds.
Arguments
frameint - The index of the frame to be converted to milliseconds.fpsint - Frames per second.
Returns
int- The rounded millisecond value of the input frame index.
framediff_ffmpeg
def framediff_ffmpeg(filename, target_name=None, color=True, overwrite=False):
Renders a frame difference video from the input using ffmpeg.
Arguments
filenamestr - Path to the input video file.target_namestr, optional - The name of the output video. Defaults to None (which assumes that the input filename with the suffix "_framediff" should be used).colorbool, optional - If False, the output will be grayscale. Defaults to True.overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- Path to the output video.
generate_outfilename
def generate_outfilename(requested_name):
Returns a unique filepath to avoid overwriting existing files. Increments requested filename if necessary by appending an integer, like "_0" or "_1", etc to the file name.
Arguments
requested_namestr - Requested file name as path string.
Returns
str- If file at requested_name is not present, then requested_name, else an incremented filename.
get_box_video_ratio
def get_box_video_ratio(filename, box_width=800, box_height=600):
Gets the box-to-video ratio between an arbitrarily defind box and the video dimensions. Useful to fit windows into a certain area.
Arguments
filenamestr - Path to the input video file.box_widthint, optional - The width of the box to fit the video into.box_heightint, optional - The height of the box to fit the video into.
Returns
int- The smallest ratio (ie. the one to use for scaling the video window to fit into the box).
get_first_frame_as_image
def get_first_frame_as_image(
filename,
target_name=None,
pict_format='.png',
overwrite=False,
):
Extracts the first frame of a video and saves it as an image using ffmpeg.
Arguments
filenamestr - Path to the input video file.target_namestr, optional - The name for the output image. Defaults to None (which assumes that the input filename should be used).pict_formatstr, optional - The format to use for the output image. Defaults to '.png'.overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- Path to the output image file.
get_fps
def get_fps(filename):
Gets the FPS (frames per second) value of a video using FFprobe.
Arguments
filenamestr - Path to the video file to measure.
Returns
float- The FPS value of the input video file.
get_frame_planecount
def get_frame_planecount(frame):
Gets the planecount (color channel count) of a video frame.
Arguments
frame (numpy array): A frame extracted by cv2.VideoCapture().read().
Returns
int- The planecount of the input frame, 3 or 1.
get_framecount
def get_framecount(filename, fast=True):
Returns the number of frames in a video using FFprobe.
Arguments
filenamestr - Path to the video file to measure.
Returns
int- The number of frames in the input video file.
get_length
def get_length(filename):
Gets the length (in seconds) of a video using FFprobe.
Arguments
filenamestr - Path to the video file to measure.
Returns
float- The length of the input video file in seconds.
get_widthheight
def get_widthheight(filename):
Gets the width and height of a video using FFprobe.
Arguments
filenamestr - Path to the video file to measure.
Returns
int- The width of the input video file.int- The height of the input video file.
has_audio
def has_audio(filename):
Checks if video has audio track using FFprobe.
Arguments
filenamestr - Path to the video file to check.
Returns
bool- True iffilenamehas an audio track, False otherwise.
in_colab
def in_colab():
Check's if the environment is a Google Colab document.
Returns
bool- True if the environment is a Colab document, otherwise False.
motiongrams_ffmpeg
def motiongrams_ffmpeg(
filename,
color=True,
filtertype='regular',
threshold=0.05,
blur='none',
use_median=False,
kernel_size=5,
invert=False,
target_name_x=None,
target_name_y=None,
overwrite=False,
):
Renders horizontal and vertical motiongrams using ffmpeg.
Arguments
filenamestr - Path to the input video file.colorbool, optional - If False the input is converted to grayscale at the start of the process. This can significantly reduce render time. Defaults to True.filtertypestr, optional - 'Regular' turns all values belowthreshto 0. 'Binary' turns all values belowthreshto 0, abovethreshto 1. 'Blob' removes individual pixels with erosion method. Defaults to 'Regular'.threshfloat, optional - Eliminates pixel values less than given threshold. Ranges from 0 to 1. Defaults to 0.05.blurstr, optional - 'Average' to apply a 10px * 10px blurring filter, 'None' otherwise. Defaults to 'None'.use_medianbool, optional - If True the algorithm applies a median filter on the thresholded frame-difference stream. Defaults to False.kernel_sizeint, optional - Size of the median filter (ifuse_median=True) or the erosion filter (iffiltertype='blob'). Defaults to 5.invertbool, optional - If True, inverts colors of the motiongrams. Defaults to False.target_name_xstr, optional - Target output name for the motiongram on the X axis. Defaults to None (which assumes that the input filename with the suffix "_mgx_ffmpeg" should be used).target_name_ystr, optional - Target output name for the motiongram on the Y axis. Defaults to None (which assumes that the input filename with the suffix "_mgy_ffmpeg" should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to False.
Returns
str- Path to the output horizontal motiongram (_mgx).str- Path to the output vertical motiongram (_mgy).
motionvideo_ffmpeg
def motionvideo_ffmpeg(
filename,
color=True,
filtertype='regular',
threshold=0.05,
blur='none',
use_median=False,
kernel_size=5,
invert=False,
target_name=None,
overwrite=False,
):
Renders a motion video using ffmpeg.
Arguments
filenamestr - Path to the input video file.colorbool, optional - If False the input is converted to grayscale at the start of the process. This can significantly reduce render time. Defaults to True.filtertypestr, optional - 'Regular' turns all values belowthreshto 0. 'Binary' turns all values belowthreshto 0, abovethreshto 1. 'Blob' removes individual pixels with erosion method. Defaults to 'Regular'.threshfloat, optional - Eliminates pixel values less than given threshold. Ranges from 0 to 1. Defaults to 0.05.blurstr, optional - 'Average' to apply a 10px * 10px blurring filter, 'None' otherwise. Defaults to 'None'.use_medianbool, optional - If True the algorithm applies a median filter on the thresholded frame-difference stream. Defaults to False.kernel_sizeint, optional - Size of the median filter (ifuse_median=True) or the erosion filter (iffiltertype='blob'). Defaults to 5.invertbool, optional - If True, inverts colors of the motion video. Defaults to False.target_namestr, optional - Defaults to None (which assumes that the input filename with the suffix "_motion" should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- Path to the output video.
pass_if_container_is
def pass_if_container_is(container, file):
Checks if a file's extension matches a desired one. Passes if so, raises WrongContainer if not.
Arguments
containerstr - The container to match.filestr - Path to the file to inspect.
Raises
WrongContainer- If the file extension (container) matches the desired one.
pass_if_containers_match
def pass_if_containers_match(file_1, file_2):
Checks if file extensions match between two files. If they do it passes, is they don't it raises WrongContainer exception.
Arguments
file_1str - First file in comparison.file_2str - Second file in comparison.
Raises
WrongContainer- If file extensions (containers) mismatch.
quality_metrics
def quality_metrics(original, processed, metric=None):
Compute video quality metrics between two video files for comparing the quality of video codecs or measuring the efficacy of encoding configuration. Possible to compute three major video quality metrics used for objective evaluation, namely:
- PSNR: It is the most commonly used video quality metric. But it has the lowest predictive value, so the results are inconsistent. Used by major platforms like Netflix and Facebook to compare different codecs and for similar use cases. Overall usage is declining.
- SSIM: Mostly used by technical experts like codec researchers and compression engineers. Usage is declining steadily. However, it has a higher predictive value than PSNR.
- VMAF: Introduced first by Netflix but then converted into an open-source asset. VMAF is easily accessible and widely used. Designed specifically for evaluating the video quality of streams encoded for multiple-resolution rungs.
Arguments
originalstr - Path to the original/reference video file.processedstr - Path to the processed/distorted video file.metricstr, optional - Type of quality metric to compute ('vmaf', 'ssim', or 'psnr'). Defaults to None (which computes all the metrics).
rotate_video
def rotate_video(filename, angle, target_name=None, overwrite=False):
Rotates a video by an angle using ffmpeg.
Arguments
filenamestr - Path to the input video file.anglefloat - The angle (in degrees) specifying the amount of rotation. Positive values rotate clockwise.target_namestr, optional - Target filename as path. Defaults to None (which assumes that the input filename with the suffix "_rot" should be used).overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- The path to the rotated video file.
roundup
def roundup(num, modulo_num):
Rounds up a number to the next integer multiple of another.
Arguments
numint - The number to round up.modulo_numint - The number whose next integer multiple we want.
Returns
int- The rounded-up number.
scale_array
def scale_array(array, out_low, out_high):
Scales an array linearly.
Arguments
arrayarraylike - The array to be scaled.out_lowfloat - Minimum of output range.out_highfloat - Maximum of output range.
Returns
arraylike- The scaled array.
scale_num
def scale_num(val, in_low, in_high, out_low, out_high):
Scales a number linearly.
Arguments
valfloat - The value to be scaled.in_lowfloat - Minimum of input range.in_highfloat - Maximum of input range.out_lowfloat - Minimum of output range.out_highfloat - Maximum of output range.
Returns
float- The scaled number.
str2sec
def str2sec(time_string):
Converts a time code string into seconds.
Arguments
time_stringstr - The time code to convert. Eg. '01:33:42'.
Returns
float- The time code converted to seconds.
threshold_ffmpeg
def threshold_ffmpeg(
filename,
threshold=0.1,
target_name=None,
binary=False,
overwrite=False,
):
Renders a pixel-thresholded video from the input using ffmpeg.
Arguments
filenamestr - Path to the input video file.thresholdfloat, optional - The normalized pixel value to use as the threshold. Pixels below the threshold will turn black. Defaults to 0.1.target_namestr, optional - The name of the output video. Defaults to None (which assumes that the input filename with the suffix "_thresh" should be used).binarybool, optional - If True, the pixels above the threshold will turn white. Defaults to False.overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False.
Returns
str- Path to the output video.
unwrap_str
def unwrap_str(string):
Unwraps a string from quotes.
Arguments
stringstr - The string to inspect.
Returns
str- The (unwrapped) string.
wrap_str
def wrap_str(string, matchers=[' ', '(', ')']):
Wraps a string in double quotes if it contains any of matchers - by default: space or parentheses.
Useful when working with shell commands.
Arguments
stringstr - The string to inspect.matcherslist, optional - The list of characters to look for in the string. Defaults to [" ", "(", ")"].
Returns
str- The (wrapped) string.