foley.sources.media#
Media inputs of generation (#74, #162, #215): what video= and an edit’s audio= may be, and their identity.
A backend that sounds a picture (video-to-audio) takes a video=; a backend that
edits a source clip takes an audio=. Both are media inputs (MEDIA,
MEDIA_INPUTS) and take the same forms: a URL (http(s):// or a
data: URI, sent as is), a RemoteVideo (a URL with the SHA-256 of its
content), a local path, or raw bytes (both uploaded by the backend’s adapter).
These questions are answered here, once, for every media input:
media_key(): what the generations cache keys the input as. The content (sha256:<hex>of the bytes, the file or aRemoteVideo’s digest), never its path, so a moved file hits the cache and an edited one cannot; a bare remote URL is keyed as the URL.is_media_key(): whether a value is already such a key (as a cache entry keeps it).replayable(): whether an identical request may be served from the cache. Not for a bare remote URL: the content behind it may have changed (#173).recorded_media_key(): the key as it is written down (a cache entry, a run manifest, a licence): a bare URL without its query, fragment or credentials, so a presigned URL’s token never reaches a record (#184). The request digest still hashesmedia_key().media_url(): the URL a backend fetches the input from itself, if it is one.local_media_file(): a local file for an uploader (bytes are written to a temporary file whose suffix is sniffed from them:video_suffix(), or the input’s own sniffer,MediaInput.suffix()).keyed(): affordances as they may be recorded (the cache entry, the run manifest): each media input replaced by its recorded key, so neither its bytes nor a local path (nor an upload’s capability URL, nor a presigned URL’s token) is written down.
Each media input is one row of MEDIA (its mode, its upload suffix sniffer, its
length probe): a video’s suffix is sniffed by video_suffix() and its length read
by foley.sources.video.video_seconds(); an edit’s source clip’s by
audio_suffix() and audio_seconds().
What only a video has (its length by ffprobe/mixing, the audio track of a muxed
answer) is in foley.sources.video, which also keeps this module’s helpers
importable under their former video_* names.
Stdlib only (soundfile and falaw are used when installed, never required).
Module Attributes
the media inputs a backend is given (keyed by content, never by path), in precedence order (a video decides the mode over an audio). |
|
The affordances of |
|
Schemes of a URL the backend fetches from a server (what it serves may change). |
|
URL schemes sent to a backend as is (no upload); a |
|
The key prefix of a media input known only by its URL (its content unseen). |
|
a URL, a |
|
What stands in for a recorded URL's query and fragment. |
|
The suffix of video bytes no signature matches (and of every other ISO BMFF brand). |
|
The suffix of audio bytes no signature matches. |
Functions
|
The length of an audio input (bytes or a local path) in seconds, or |
|
The file suffix of encoded audio |
|
falaw's |
|
Whether |
|
Whether |
|
|
|
A local path holding |
|
The cache identity of |
|
The URL a backend fetches |
|
|
|
The name of the first media input given as a bare remote URL, or |
|
Whether an identical request may be served from the cache (#173). |
|
The file suffix of encoded video |
Classes
|
One media input of generation (a row of |
|
A remote video |
- foley.sources.media.DEFAULT_AUDIO_SUFFIX = '.wav'#
The suffix of audio bytes no signature matches.
- foley.sources.media.DEFAULT_VIDEO_SUFFIX = '.mp4'#
The suffix of video bytes no signature matches (and of every other ISO BMFF brand).
- foley.sources.media.MEDIA: dict[str, MediaInput] = {'audio': MediaInput(name='audio', mode='edit', does='edits a source clip', covers='to match the source clip', suffix_of='media.audio_suffix', seconds_of='media.audio_seconds'), 'video': MediaInput(name='video', mode='v2a', does='sounds a picture', covers='to cover the video', suffix_of='media.video_suffix', seconds_of='video.video_seconds')}#
the media inputs a backend is given (keyed by content, never by path), in precedence order (a video decides the mode over an audio).
- Type:
SSOT
- class foley.sources.media.MediaInput(name, mode, does, covers, suffix_of, seconds_of)[source]#
Bases:
objectOne media input of generation (a row of
MEDIA): the affordance that carries it, the mode it puts a call in, and how foley reads it.- suffix_of#
The sniffer of its bytes’ file suffix, as
'<module>.<name>'infoley.sources.- Type:
- seconds_of#
The probe of its length, likewise. Both are looked up when called, so a test can stand in for the probe.
- Type:
- property needed: str#
What a source taking it says when a call lacks it.
>>> MEDIA["audio"].needed 'needs an audio= (it edits a source clip)'
- foley.sources.media.MediaValue#
a URL, a
RemoteVideo, a local path, or the bytes.- Type:
What a media input accepts
alias of
str|RemoteVideo|os.PathLike[str]|bytes
- foley.sources.media.REDACTED_QUERY = '?…'#
What stands in for a recorded URL’s query and fragment.
- foley.sources.media.REMOTE_SCHEMES = ('http://', 'https://')#
Schemes of a URL the backend fetches from a server (what it serves may change).
- class foley.sources.media.RemoteVideo(*, url, sha256)[source]#
Bases:
objectA remote video
urlwith the SHA-256 of its bytes: keyed, and replayed, by content.A bare
https://URL is keyed as the URL, which foley cannot see behind, so an identical request for it is never served from the generations cache (the picture may have changed:.../latest.mp4). A caller who knows the content passes this instead, and a repeat is replayed while the digest is the same (#173).The digest is trusted, not checked (foley does not fetch the URL): it is recorded as the input’s identity in the cache key and the licence’s
generation_params, as a digest foley computed would be.>>> RemoteVideo(url="https://x/clip.mp4", sha256="sha256:" + "ab" * 32).sha256[:4] 'abab' >>> RemoteVideo(url="clip.mp4", sha256="ab" * 32) Traceback (most recent call last): ... ValueError: RemoteVideo.url must be an http(s) URL, got 'clip.mp4'
- foley.sources.media.URL_KEY_PREFIX = 'url:'#
The key prefix of a media input known only by its URL (its content unseen).
- foley.sources.media.URL_SCHEMES = ('http://', 'https://', 'data:')#
URL schemes sent to a backend as is (no upload); a
data:URI holds its content.
- foley.sources.media.audio_seconds(audio)[source]#
The length of an audio input (bytes or a local path) in seconds, or
None.Nonefor a URL (not fetched), withoutsoundfile, or when it cannot be read.
- foley.sources.media.audio_suffix(data)[source]#
The file suffix of encoded audio
data(flac, wav, mp3, ogg), sniffed from its first bytes.Anything unrecognised is
DEFAULT_AUDIO_SUFFIX.>>> audio_suffix(b"fLaC\x00"), audio_suffix(b"RIFF\x00\x00\x00\x00WAVEfmt ") ('.flac', '.wav') >>> audio_suffix(b"ID3\x04"), audio_suffix(b"\xff\xfb\x90"), audio_suffix(b"OggS") ('.mp3', '.mp3', '.ogg') >>> audio_suffix(b"?") '.wav'
- Return type:
- foley.sources.media.fal_immutable_url(url)[source]#
falaw’s
is_immutable_url(fal’s own CDN mints a new URL per upload), elseFalsewhen falaw is not installed: an unknown URL is taken as mutable.- Return type:
- foley.sources.media.is_media_key(value)[source]#
Whether
valueis already a keymedia_key()makes (as a cache entry keeps it).>>> is_media_key("sha256:" + "ab" * 32), is_media_key("url:https://x/a.mp4") (True, True) >>> is_media_key("clip.mp4"), is_media_key(b"sha256:") (False, False)
- Return type:
- foley.sources.media.is_url(media)[source]#
Whether
mediais a URL a backend can fetch itself.>>> is_url("https://x/clip.mp4"), is_url("clip.mp4"), is_url(b"...") (True, False, False)
- Return type:
- foley.sources.media.keyed(affordances, *, key=<function recorded_media_key>)[source]#
affordanceswith each media input (MEDIA_INPUTS) replaced by itskey.The default is the key as it is written down (
recorded_media_key()); the request digest passeskey=media_key, the full identity, which is only hashed. Unchanged when the call has no media input.>>> keyed({"video": b"abc", "duration_s": 2})["video"][:15], keyed({"seed": 1}) ('sha256:ba7816bf', {'seed': 1}) >>> keyed({"audio": b"abc"})["audio"] == keyed({"video": b"abc"})["video"] True
- Return type:
- foley.sources.media.local_media_file(media, *, suffix=None)[source]#
A local path holding
media(bytes are written to a temporary file).suffixnames the temporary file’s format; default:video_suffix()of the bytes. A path is yielded as is.
- foley.sources.media.media_key(media)[source]#
The cache identity of
media: its content digest, or a bare remote URL as is.>>> media_key(b"abc")[:15] 'sha256:ba7816bf' >>> media_key("https://x/clip.mp4") 'url:https://x/clip.mp4' >>> media_key(RemoteVideo(url="https://x/clip.mp4", sha256="ab" * 32))[:11] 'sha256:abab'
- Raises:
FileNotFoundError – If
medianames a local file that does not exist.TypeError – If
mediais none of a URL, a path or bytes.
- Return type:
- foley.sources.media.media_url(media)[source]#
The URL a backend fetches
mediafrom itself, orNone(a path or bytes).>>> media_url("https://x/clip.mp4"), media_url("clip.mp4"), media_url(b"...") ('https://x/clip.mp4', None, None)
- foley.sources.media.recorded_media_key(media)[source]#
media_key()as it is written down: a bare URL keeps its scheme, host and path only (#184): its query, fragment and;path parameters (a presigned URL’s signature, a session id) becomeREDACTED_QUERY, and credentials before the host are dropped. Two URLs that differ only there record (and are found byfind()) as one, as a re-signed URL should. An already recorded or bareurl:key is redacted the same way.>>> recorded_media_key("https://u:p@bucket.s3.example/a.mp4?X-Amz-Signature=s3cr3t") 'url:https://bucket.s3.example/a.mp4?…' >>> recorded_media_key("https://[::1]:8080/a.mp4;jsessionid=X#t=2") 'url:https://[::1]:8080/a.mp4?…' >>> recorded_media_key("https://x/a.mp4"), recorded_media_key(b"abc")[:11] ('url:https://x/a.mp4', 'sha256:ba78')
- Return type:
- foley.sources.media.remote_media_input(affordances)[source]#
The name of the first media input given as a bare remote URL, or
None.>>> remote_media_input({"audio": "https://x/a.wav"}), remote_media_input({"video": b"x"}) ('audio', None)
- foley.sources.media.replayable(affordances, *, is_immutable=<function fal_immutable_url>)[source]#
Whether an identical request may be served from the cache (#173).
Not when a media input (
MEDIA_INPUTS) is a bare remote URL whose content foley never saw, unlessis_immutable(url)says it never serves other bytes (default:fal_immutable_url(), fal’s own CDN). Decided from the values alone: no file is read.>>> replayable({"video": "https://x/latest.mp4"}), replayable({"video": b"abc"}) (False, True) >>> replayable({"video": "https://x/a.mp4"}, is_immutable=lambda url: True) True >>> replayable({"seed": 1}), replayable({"audio": "https://x/take.wav"}) (True, False)
- Return type:
- foley.sources.media.video_suffix(data)[source]#
The file suffix of encoded video
data, sniffed from its first bytes.An uploader (and ffmpeg) names the format by the suffix, so a WebM written as
.mp4goes out mislabelled. Anything unrecognised is.mp4.>>> video_suffix(b"\x00\x00\x00\x18ftypqt \x00"), video_suffix(b"\x00\x00\x00\x18ftypisom") ('.mov', '.mp4') >>> video_suffix(b"\x1a\x45\xdf\xa3\x9f\x42\x82\x84webm"), video_suffix(b"\x1a\x45\xdf\xa3") ('.webm', '.mkv') >>> video_suffix(b"RIFF\x00\x00\x00\x00AVI "), video_suffix(b"OggS"), video_suffix(b"?") ('.avi', '.ogv', '.mp4')
- Return type: