an.music_library#
Acquire a licensed music library, with provenance: an’s bridge to foley (an#470, an#656).
The library itself lives in foley (foley.sources.music_library): the
registry (incompetech, and the paid artlist and epidemic_sound,
whose titles become wishlist rows), the direct and Wayback mirrors, the polite
fetch with 429 / Retry-After resilience, the audio probe, the licence
gate, the tracks table and the credit parser. an music add-library
(an.music_cli) is its command:
an music add-library incompetech –titles-from usage.json –out audio/macleod
What stays here is what is an’s:
asset_source(), the bridge: one foley row (anAcquiredTrack, or its.row()readback from a tracks table) as an
an.ir.assets.AssetSource, the provenance a sound in the project’s store carries. foley’s licence ids become an’s codes (license_code(), foley’s own mapping,foley.licensing.consumer_license_code:CC-BY-4.0iscc-by-4.0; foley’sunknown, a paid library’s, isNone, which an reads as UNKNOWN); the library’s own address issource_page_url;rights_verified, the mirror and a titles file’s metadata ride as extras.acquire(), foley’s with an’spauseknob (seconds before a second request to one host), which becomes foley’s per-hostrate.add_track_to_sounds()(an#623): one acquired row’s verified file, cut, into the project’s sounds store with the row as its provenance, so a film uses the track without fetching it again (an music use).
The rest of foley’s surface the command and its tests use (MUSIC_LIBRARIES,
tracks_table(), attribution_text(), titles_by_library(), and the
tracks-table reader read_tracks_table() and LIST_SEPARATOR, an#731) is re-exported by name, and loaded on first use:
import an never imports foley.
>>> license_code("CC-BY-4.0"), license_code("unknown")
('cc-by-4.0', None)
>>> src = asset_source({"title": "Sneaky Snitch", "library": "incompetech",
... "status": "acquired", "license": "CC-BY-4.0",
... "source_page_url": "https://incompetech.com/x.mp3",
... "rights_verified": True, "mirror": "wayback"})
>>> src.provider, src.id, src.license, src.source_page_url
('incompetech', 'Sneaky Snitch', 'cc-by-4.0', 'https://incompetech.com/x.mp3')
>>> src.model_extra
{'rights_verified': True, 'commercial_ok': True, 'mirror': 'wayback'}
Module Attributes
a library is someone's website). |
Functions
|
foley's |
|
Put one acquired track in the sounds |
|
One foley row as an's |
|
The attribution blocks to paste under a video, one per track that has one (acquired or kept), blank-line separated, each once. |
|
|
|
|
|
|
|
foley's licence id as an's code: foley's |
|
The duration of the audio |
|
The rows of a tracks table ( |
|
Add |
|
The titles a video description credits, by library (default: the registered ones). |
|
A titles file's JSON: a list of titles, or |
|
|
|
The tracks table as CSV text: |
|
The Internet Archive's raw copy of |
Classes
|
One row of the tracks table: a title, what became of it, and its provenance. |
|
One library: who made it, under which licence, and where a title's file is. |
Exceptions
A tracks-table row named columns that set nothing, and |
|
A library, a title or a file that cannot be used, and why. |
|
A tool every title needs is missing: the batch stops before it starts. |
- class an.music_library.AcquiredTrack(title, library, status, license, file=None, sha256=None, duration_s=None, url=None, mirror=None, page_url=None, credited_by=(), metadata=dict(), error=None)[source]#
Bases:
SerializableMixinOne row of the tracks table: a title, what became of it, and its provenance.
urlis the URL that served the bytes (a mirror’s),mirrorthat mirror’s name,page_urlthe library’s own address for the title;credited_bythe videos that credit it andmetadataa titles file’s other fields (feel,genre, …).- classmethod from_row(row, *, libraries=None)[source]#
The track a tracks-table
rowrecords: the inverse ofrow().rowis arow()dict (or one read back from JSON), or a line of the table (read_tracks_table(): every cell text, an empty one nothing). Itslibraryis looked up inlibraries(defaultMUSIC_LIBRARIES) and its licence rebuilt from it (track_license()), never read from the row. The row’s licence cells (LICENSE_COLUMNS) must still say what the library states now: a cell someone edited, or a library re-registered under other terms since the table was written, is refused rather than silently widened or narrowed.A column outside
TRACK_COLUMNSis metadata, as the table wrote it (a list or mapping a titles file gave comes back as its cell’s text). A column a titles file may not set (a downstream provenance field such ascost_usdorcacheable, a licence flag) is dropped with aDroppedColumnsWarning: it never sets anything.- Raises:
MusicLibraryError – For a row with a key that is not text, a blank title, no library or status, a status not in
STATUSES, a library that is not registered, aduration_sthat is not a finite number, or licence cells that differ from the library’s (naming them).- Return type:
- an.music_library.DFLT_PAUSE_S: float = 2.0#
a library is someone’s website).
- Type:
Seconds before a second request to one host (foley’s default pace
- exception an.music_library.DroppedColumnsWarning[source]#
Bases:
UserWarningA tracks-table row named columns that set nothing, and
AcquiredTrack.from_row()dropped them.
- class an.music_library.MusicLibrary(name, author, license_url=None, track_url=None, attribution='"{title}" {author}', author_url=None, aliases=(), file_name=<function safe_file_name>)[source]#
Bases:
objectOne library: who made it, under which licence, and where a title’s file is.
license_urlis the URL of its Creative Commons licence, read bylicense_id_from_cc_url();Nonemakes the library PAID (its titles become wishlist rows, never downloads;track_urlis then unused).track_urlis thetitle -> URLrule (title_url()builds one from a template).attributionis a template over{title},{author}and{license_url}.aliasesare the names a video description credits it by (From Epidemic Sound:,Kevin MacLeod (incompetech.com));file_namethe name a title is saved under.- attribution_for(title)[source]#
The library’s attribution for
title, from its template.- Return type:
- file_name(*, suffix='.mp3')#
titleas a file name: NFC, path separators as-, no leading dot.>>> safe_file_name("AC/DC"), safe_file_name("../../escape") ('AC-DC.mp3', '-..-escape.mp3')
- Return type:
- exception an.music_library.MusicLibraryError[source]#
Bases:
ValueErrorA library, a title or a file that cannot be used, and why.
- exception an.music_library.MusicToolMissing[source]#
Bases:
RuntimeErrorA tool every title needs is missing: the batch stops before it starts.
- an.music_library.acquire(library, titles, *, out, pause=2.0, config=None, **kwargs)[source]#
foley’s
acquire(), paced bypause.pauseis the seconds before a second request to one host (0: no pause), foley’srateunlessconfigstates its own.configand every other keyword (fetch,mirrors,verify,kept,intended_use,clock,sleep, …) go to foley as they are. Bridge the rows withasset_source().- Raises:
MusicLibraryError – For a negative
pause, and whatever foley’sacquirerefuses.- Return type:
- an.music_library.add_track_to_sounds(store, key, track, files, *, start=None, duration=None, fade_out=None, description='')[source]#
Put one acquired track in the sounds
storeunderkey(an#623).trackis foley’sAcquiredTrack, or its tracks-table row (atracks.csvline,track_row(): its licence is rebuilt from the library’s registry,AcquiredTrack.from_row), andfilesthe library folder as{file name: bytes}(files_store(out)). The file it names is checked against itssha256, cut withan.sound_fetch.cut_to_wav()(start,duration,fade_out, asan sounds addtakes them) and stored with the track as itsAssetSource(asset_source()) plus the cut, the file’s digest asfile_sha256, soan creditslists the music with its licence. Nothing is fetched again: the site may be the one that refused scripts.- Raises:
MusicLibraryError – For a track that holds no file (not acquired or kept, or no
file/sha256), a file missing fromfiles, one whose bytes do not match the recorded digest, or an unknown library.- Return type:
- an.music_library.asset_source(track)[source]#
One foley row as an’s
AssetSource.trackis a foleyAcquiredTrackor its.row()(as a tracks table reads back), which is rebuilt first (AcquiredTrack.from_row, an#731): its licence comes from the library’s registry, and a licence cell that says otherwise is REFUSED, so an edited row cannot grant rights.libraryis theprovider,titletheid, the licence id an’s code (license_code());url(the mirror that served the bytes),source_page_url(the library’s own address), the licence URL, author, attribution andsha256keep their names. The acquisition’s own columns (ROW_ONLY) stay in the table; every other column (rights_verified,commercial_ok,mirror,credited_by, a titles file’s metadata) rides as an extra; a column named like one of the source’s own fields (cost_usd,cacheable,id, …) is dropped, never trusted. Empty cells are dropped. The licence’screator_urlis theauthor_urland whether its terms let the bytes be kept ascacheable.- Return type:
- an.music_library.attribution_text(rows)[source]#
The attribution blocks to paste under a video, one per track that has one (acquired or kept), blank-line separated, each once.
Each block is
foley.provenance.credits.attribution_line()of the row’s licence: the library’s own wording, verbatim.>>> from foley.sources.music_library import MUSIC_LIBRARIES, track_license >>> lic = track_license(MUSIC_LIBRARIES["incompetech"], "A") >>> print(attribution_text([AcquiredTrack("A", "incompetech", ACQUIRED, lic)] * 2)) "A" Kevin MacLeod (incompetech.com) Licensed under Creative Commons: By Attribution 4.0 License http://creativecommons.org/licenses/by/4.0/
- Return type:
- an.music_library.files_store(out)[source]#
outas a{file name: bytes}store: a mapping is used as it is; a folder becomes foley’s file store over it (foley.stores.make_byte_store(), which makes the folder on its first write).- Return type:
- an.music_library.kept_digests(table)[source]#
{file: sha256}of the acquired or kept rows of a tracks table: what a rerun may keep.tableistracks_table()’s CSV text, or the rows themselves (AcquiredTrackor theirrow()dicts, read back from JSON).
- an.music_library.kept_sources(table)[source]#
{file: {"sha256", "url", "mirror"}}of the acquired or kept rows of a tracks table:kept_digests()with where each file came from, so a kept row still says which URL and mirror served it (acquire(kept=...)takes either).
- an.music_library.license_code(license_id)[source]#
foley’s licence id as an’s code: foley’s
consumer_license_code, the one mapping (an#729), imported on first use.>>> license_code("CC0-1.0"), license_code("unknown"), license_code(None) ('cc0-1.0', None, None)
- an.music_library.probe_verify(data, *, probe=None, has_audio=None, suffix='.mp3')[source]#
The duration of the audio
data, in seconds.The bytes are written to a temporary file named with
suffix(from a pipe, ffprobe reports no duration).has_audio(path) -> True | False | Nonemust sayTrue(defaulthas_audio_stream();None, “cannot tell”, is a refusal), thenprobe(path) -> seconds | Nonereads the length (defaultdefault_probe()).>>> probe_verify(b"ID3...", probe=lambda path: 61.5, has_audio=lambda path: True) 61.5 >>> probe_verify(b"<html>403</html>", probe=lambda path: 3.0, ... has_audio=lambda path: False) Traceback (most recent call last): ... foley.sources.music_library.libraries.MusicLibraryError: the file has no audio stream >>> probe_verify(b"ID3...", probe=lambda path: None, has_audio=lambda path: True) Traceback (most recent call last): ... foley.sources.music_library.libraries.MusicLibraryError: the file has no readable duration (not audio)
- Raises:
MusicLibraryError – When the file is empty, has no audio stream (or one cannot be confirmed), or has no readable duration.
- Return type:
- an.music_library.read_tracks_table(text)[source]#
The rows of a tracks table (
tracks_table()’s CSV text), as written; a byte-order mark an editor added (Excel does) is not part of the first column.>>> read_tracks_table("\ufefftitle,library\r\nA,incompetech\r\n") [{'title': 'A', 'library': 'incompetech'}]
- Raises:
MusicLibraryError – For a header that names a column twice, or a line with more cells than the header has columns (naming its line).
- Return type:
- an.music_library.register_music_library(library, *, replace=False)[source]#
Add
librarytoMUSIC_LIBRARIES(replaceto redefine one).A free library is checked for one licence right only, keeping the files (
cache_bytes_ok); the uses a production makes of them are gated per title byacquire()(intended_use).A library’s name is also its live-source name (
foley.add_from(name, ...)), so it may not be the name of a source anotherfoley.sourcesmodule declares, or one registered directly (openverse,freesound, afal_*endpoint, …): that library would silently take the source over, or silently never become one.- Raises:
MusicLibraryError – If the name is taken (and not
replace), or a free library has notrack_urlrule or a licence that does not allow keeping the files (a ShareAlike / NoDerivatives licence, refused on purpose; a licence URL foley cannot read; a licence whose flags say no).SourceNameCollision – If the name is another module’s source name.
- Return type:
- an.music_library.titles_by_library(description, *, libraries=None)[source]#
The titles a video description credits, by library (default: the registered ones).
A line naming a library (by name or alias, with
From/Music by/Music:and a parenthetical allowed around it) opens its block, and each following line is a title until a blank line; a timestamp, aSong:/Track:label, quotes and bullets are stripped, and links and licence lines skipped. The attribution form ("Title" Kevin MacLeod (incompetech.com)) credits its title wherever it stands.>>> text = '''Music: ... From Epidemic Sound: ... - 0:00 Night Owl ... - Song: "Morning Light" ... https://www.epidemicsound.com/ ... ... "Sneaky Snitch" Kevin MacLeod (incompetech.com) ... Licensed under Creative Commons: By Attribution 4.0 License''' >>> titles_by_library(text) {'epidemic_sound': ['Night Owl', 'Morning Light'], 'incompetech': ['Sneaky Snitch']}
- an.music_library.titles_from_json(text)[source]#
A titles file’s JSON: a list of titles, or
{title: {metadata}}.>>> titles_from_json('["A", "B"]'), titles_from_json('{"A": {"feel": "Bright"}}') (['A', 'B'], {'A': {'feel': 'Bright'}})
- an.music_library.track_row(text, title)[source]#
title’s row in the tracks tabletext.- Raises:
MusicLibraryError – When no row has that title, naming the ones there are.
- Return type:
- an.music_library.tracks_table(rows)[source]#
The tracks table as CSV text:
TRACK_COLUMNS, then any metadata the rows carry, in name order (read_tracks_table()reads it back).- Return type: