video_utils.audio package
Submodules
video_utils.audio.audio_delay module
Determine audio delay between files
Using the audio_delay() function, the delay between audio files can be determined. This is done using the cross-correlation between the two files and using the lag/lead at which the correlation is the largest.
There is also a function that can be used to visualize the delay between the two files, and the accuracy of this method at aligning them.
- video_utils.audio.audio_delay.audio_delay(file1, file2, show_plots: bool = False, limit: int | None = None)
Compute delay between audio in different video files
Motivation behind this function was to add surround sound tracks to videos that only contined stereo downmixes, but were higher video resolution (say 720p) than the source of the surround tracks (say 480p from DVD).
- Parameters:
file1 (str) – Path to file to align to
file2 (str) – Path to file to align
- Keyword Arguments:
show_plots (bool) – If true, try to display plots of alignment
limit (int) – Length of audio file (in minutes) to use to align
- Returns:
Delay (in seconds) as float and as formatted string
- Return type:
tuple
- video_utils.audio.audio_delay.fft_xcorr(samples1, samples2)
Compute scaled cross-correlation using ffts
Compute the cross-correlation between two audio files to determine time delay
- Parameters:
samples1 (numpy.ndarray) – Samples from first audio file
samples2 (numpy.ndarray) – Samples from second audio file
- video_utils.audio.audio_delay.plot_signals(signal1, signal2, fs, delay_mean)
Function to plot the signals to show the alignment.
video_utils.audio.dolby_downmix module
Downmixing of surround audio
- video_utils.audio.dolby_downmix.dolby_downmix(infile, outdir: str | None = None, pro_logic_2: bool = True, aac: bool = False, flac: bool = False, time: str | None = None)
Downmix surround sound channels (5.1+) to Dolby Pro Logic II
A function that downmixes surround sound channels (5.1+) in a video file to Dolby Pro Logic II.
Information can be found starting in section 7.8 on page 91 of http://www.atsc.org/wp-content/uploads/2015/03/A52-201212-17.pdf and at: http://forum.doom9.org/showthread.php?s=&threadid=27936
- Parameters:
infile (str) – Full path to the file that should be downmixed
- Keyword Arguments:
pro_logic_2 (bool) – Set to downmix to Dolby Pro Logic II. This is the default. Setting to False will downmix in Dolby Pro Logic.
flac (bool) – Set to output as FLAC encoded file. Default is OGG.
aac (bool) – Set to output as AAC encoded file. Default is OGG.
time (str) – String in format HH:MM:SS.0 specifing the length of the downmix. Default is complete stream.
- Returns:
- A stereo downmix of audio tracks in infile with the same name
as input. Only first audio stream is downmixed
- Return type:
None
- video_utils.audio.dolby_downmix.get_downmix_filter(pro_logic_2: bool = True)
Get format string for downmix filter
Function to return an filter string for FFmpeg audio filter to downmix surround audio channels to Dolby Pro Logic or Dolby Pro Logic II
- Parameters:
None –
- Keyword Arguments:
pro_logic_2 – Set to use Dolby Pro Logic II downmix. This is the default
- Returns:
Format string for filter
- Return type:
str
video_utils.audio.replace_audio_streams module
Replace audio stream(s) in video file
Given two video files, replace the audio streams in one with the streams from another. Audio from the second file is time aligned to the first file to reduce/eliminate audio/video sync issues.
- video_utils.audio.replace_audio_streams.compute_offset(in1: str, in2: str, info1: dict, info2: dict, outdir: str)
Function to get offset between files.
- video_utils.audio.replace_audio_streams.extract_audio(info: dict, infile: str, outdir: str, result: list, time: timedelta | None = None)
A function to extract an audio stream from a video file
- Parameters:
info – Information about the audio strem to extract
infile – Full path to the file to extract the stream from
- Keyword Arguments:
time (timedelta) – How long the extracted stream should be.
lossless (bool) – Set to True to extract into a FLAC file. Only valid when stream is > 2 channels.
- Returns:
Path to the extracted audio stream
- Return type:
str
- video_utils.audio.replace_audio_streams.file_name_info(infile: str, info: dict | None = None)
A function to get information for naming a file
- Parameters:
infile (str) – Full path to the file to get information for
- Keyword Arguments:
None –
- Returns:
List with file information
- Return type:
list
- video_utils.audio.replace_audio_streams.replace_audio_streams(in1: str, in2: str, outdir: str | None = None, replace: bool = False)
Replace audio streams in one file with those from another.
This function will replace all audio streams in in1 with those in in2. If all audio streams in in2 are surround (i.e., > 2 channels), one downmixed stream will also be placed in in1.