Media module¶
This is a module for media processing.
For common media code samples, please refer to Media processing recipes.
Media processor module for cutting, converting media contents (audio, video, etc.)
- speach.media.concat(text, outfile, dir=None, *args, **kwargs)[source]¶
Process a ffmpeg demuxer file and write result to outfile
Read more: https://trac.ffmpeg.org/wiki/Concatenate
- Parameters
text (str) – demuxer content string, which will be written to a temporary file before calling
outfile – path to an output file
dir – The directory to create the temp demuxer file, leave as None to use Python default temp dir
- speach.media.convert(infile, outfile, *args, ffmpeg_path=None)[source]¶
Convert an audio/video file into another format
To convert the file
test.wav
in Music folder under current user’s home directory intooutput.ogg
>>> from speach import media >>> media.convert("~/Music/test.wav", "~/Music/output.ogg")
- speach.media.cut(infile, outfile, from_ts=None, to_ts=None, use_concat=False, *args, **kwargs)[source]¶
Cut a media file from a timestamp to another timestamp
To cut myfile.wav from
00:03:12
to the end of the file and write output to outfile.ogg>>> media.cut("myfile.wav", "outfile.ogg", from_ts="00:03:12")
To cut myfile.wav from the beginning to
00:04:27
and then write output to outfile.ogg>>> media.cut("myfile.wav", "outfile.ogg", to_ts="00:04:27")
When use_concat is set to True, both from_ts and to_ts must be specified
>>> media.cut("myfile.wav", "outfile.ogg", from_ts="00:03:12", to_ts="00:04:27", use_concat=True)
- Parameters
infile – Path to an existing file (in str or Path-like object)
outfile – Path to output file (must not exist, or else a FileExistsError will be raised)
from_ts (a timestamp string or a TimeSlot object) – Leave as None to start cutting from the beginning
to_ts (a timestamp string or a TimeSlot object) – Timestamp to end cutting. Leave as None to cut to the end of the file
use_concat – Set to True to use demuxer to cut audio file. Both from_ts and to_ts must be specified when use. Defaulted to None