Skip to content

Commit

Permalink
Breaking xml changes, and AlbumImage
Browse files Browse the repository at this point in the history
  • Loading branch information
apb2006 committed Jan 10, 2016
1 parent 11d1d2a commit 30ab0c8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 52 deletions.
Binary file not shown.
29 changes: 26 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@
An XQuery library to extract mp3 metadata as XML using the
[mpatric/mp3agic library](https://github.com/mpatric/mp3agic)
The output xml format matches the o/p of the [Xmlcalabash](http://xmlcalabash.com/) metadata extension.

Use:
''''
mp3agic:read($f)
''''
Result
````
<metadata>
<tag name=".." dir=".." type="..">value</tag>
<tag ..
<metadata src="C:\Users\andy\git\mp3tag-extract\src\test\v24tagswithalbumimage.mp3">
<tag dir="file" name="ChannelMode">Joint stereo</tag>
<tag dir="file" name="SampleRate">44100</tag>
<tag dir="file" name="Bitrate">125</tag>
<tag dir="file" name="Layer">III</tag>
<tag dir="file" name="Version">1.0</tag>
<tag dir="file" name="isVbr">true</tag>
<tag dir="Id3" name="Composer">COMPOSER23456789012345678901234</tag>
<tag dir="Id3" name="Artist">ARTIST123456789012345678901234</tag>
<tag dir="Id3" name="Copyright">COPYRIGHT2345678901234567890123</tag>
<tag dir="Id3" name="Track">1</tag>
<tag dir="Id3" name="Url">URL2345678901234567890123456789</tag>
<tag dir="Id3" name="Album">ALBUM1234567890123456789012345</tag>
<tag dir="Id3" name="OriginalArtist">ORIGARTIST234567890123456789012</tag>
<tag dir="Id3" name="Title">TITLE1234567890123456789012345</tag>
<tag dir="Id3" name="GenreDescription">Pop</tag>
<tag dir="Id3" name="AlbumImageMimeType">image/png</tag>
<tag dir="Id3" name="Encoder">ENCODER234567890123456789012345</tag>
<tag dir="Id3" name="Year">2014</tag>
<tag dir="Id3" name="Comment">COMMENT123456789012345678901</tag>
</metadata>
````
BaseX 8.2 or greater is required.
Expand Down
87 changes: 40 additions & 47 deletions src/main/content/mp3magic.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,36 @@ declare default function namespace 'expkg-zone58.audio.mp3';
declare namespace Mp3File="java:com.mpatric.mp3agic.Mp3File";
declare namespace ID3v1Tag="java:com.mpatric.mp3agic.ID3v1Tag";
declare namespace ID3v2Tag="java:com.mpatric.mp3agic.ID3v2";
declare namespace ID3Wrapper="java:com.mpatric.mp3agic.ID3Wrapper";

declare variable $mp3agic:v0:=map{
"length":Mp3File:getLengthInSeconds#1,
"samplerate":Mp3File:getSampleRate#1,
"version":Mp3File:getVersion#1,
"layer":Mp3File:getLayer#1,
"bitrate":Mp3File:getBitrate#1,
"Length":Mp3File:getLengthInSeconds#1,
"SampleRate":Mp3File:getSampleRate#1,
"Version":Mp3File:getVersion#1,
"Layer":Mp3File:getLayer#1,
"Bitrate":Mp3File:getBitrate#1,
"isVbr":Mp3File:isVbr#1,
"channelmode":Mp3File:getChannelMode#1
"ChannelMode":Mp3File:getChannelMode#1
};

declare variable $mp3agic:v1tags:=map{
"artist":ID3v1Tag:getArtist#1,
"album":ID3v1Tag:getAlbum#1,
"track":ID3v1Tag:getTrack#1,
"title":ID3v1Tag:getTitle#1
declare variable $mp3agic:id3tags:=map{
"Artist":ID3Wrapper:getArtist#1,
"Album":ID3Wrapper:getAlbum#1,
"Track":ID3Wrapper:getTrack#1,
"Title":ID3Wrapper:getTitle#1,
"Year":ID3Wrapper:getYear#1,
"GenreDescription":ID3Wrapper:getGenreDescription#1,
"Comment":ID3Wrapper:getComment#1,
"Composer":ID3Wrapper:getComposer#1,
"OriginalArtist":ID3Wrapper:getOriginalArtist#1,
"Copyright":ID3Wrapper:getCopyright#1,
"Url":ID3Wrapper:getUrl#1,
"Encoder":ID3Wrapper:getEncoder#1,
"AlbumImageMimeType":ID3Wrapper:getAlbumImageMimeType#1
};

declare variable $mp3agic:v2tags:=map{
"artist":ID3v2Tag:getArtist#1,
"album":ID3v2Tag:getAlbum#1,
"track":ID3v2Tag:getTrack#1,
"title":ID3v2Tag:getTitle#1,
"year":ID3v2Tag:getYear#1,
"genre":ID3v2Tag:getGenre#1,
"genredescription":ID3v2Tag:getGenreDescription#1,
"comment":ID3v2Tag:getComment#1,
"composer":ID3v2Tag:getComposer#1,
"publisher":ID3v2Tag:getPublisher#1,
"originalartist":ID3v2Tag:getOriginalArtist#1,
"albumartist":ID3v2Tag:getAlbumArtist#1,
"copyright":ID3v2Tag:getCopyright#1,
"url":ID3v2Tag:getUrl#1,
"encoder":ID3v2Tag:getEncoder#1
};

declare variable $mp3agic:dirs:=map{
"file":$mp3agic:v0,
"Id3v1":$mp3agic:v1tags,
"Id3v2": $mp3agic:v2tags
};


(:~
: tag details from file name trapping all errors
Expand All @@ -73,26 +62,30 @@ declare function read($f as xs:string) as element(metadata)
declare function tags($file as xs:string) as element(tag)*
{
let $mp3:=Mp3File:new($file)
return (extract-tags($mp3agic:v0,$mp3,"file"),
id3v1Tag($mp3),
id3v2Tag($mp3)
return (extract-tags( $mp3agic:v0,$mp3,"file"),
id3Tags($mp3)
)
};

declare function id3v1Tag($mp3) as element(tag)*
(:~
: @return standard id3 tags
:)
declare function id3Tags($mp3) as element(tag)*
{
if(Mp3File:hasId3v1Tag($mp3)) then
let $id3v1:=Mp3File:getId3v1Tag($mp3)
return extract-tags($mp3agic:v1tags,$id3v1,"Id3v1")
else ()
let $id3v1:=Mp3File:getId3v1Tag($mp3)
let $id3v2:=Mp3File:getId3v2Tag($mp3)
let $id3wrapper := ID3Wrapper:new($id3v1, $id3v2)

return extract-tags($mp3agic:id3tags,$id3wrapper,"Id3")
};

declare function id3v2Tag($mp3) as element(tag)*
(:~
: @return standard id3 tags
:)
declare function AlbumImage($mp3) as xs:hexBinary?
{
if(Mp3File:hasId3v2Tag($mp3)) then
let $id3v2:=Mp3File:getId3v2Tag($mp3)
return extract-tags($mp3agic:v2tags,$id3v2,"Id3v2")
else ()
let $id3v2:=Mp3File:getId3v2Tag($mp3)
return convert:bytes-to-hex(ID3v2Tag:getAlbumImage($id3v2))
};

(:~
Expand All @@ -107,7 +100,7 @@ declare %private function extract-tag($src,
return if($v) then element tag {
attribute dir {$dir},
attribute name {$name},
$v
fn:trace($v)
} else ()
};

Expand Down
2 changes: 1 addition & 1 deletion src/main/expath-pkg.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<package xmlns="http://expath.org/ns/pkg"
name="http://expkg-zone58.github.io/mp3tag-extractor"
abbrev="mp3tag-extractor"
version="0.2.0"
version="0.3.0"
spec="1.0">

<title>Extract mp3 metadata using https://github.com/mpatric/mp3agic</title>
Expand Down
3 changes: 2 additions & 1 deletion src/test/mp3.xq
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ declare variable $f:="v24tagswithalbumimage.mp3";
let $f:=file:resolve-path($f,file:base-dir() )
let $mp3:=Mp3File:new($f)
let $x:=<mp3><length>{Mp3File:getSampleRate($mp3)}</length></mp3>
return mp3agic:detail($f)
let $f:= mp3agic:AlbumImage($mp3)
return $f

0 comments on commit 30ab0c8

Please sign in to comment.