文档章节

erlng file id3v1 id3v1.1

x
 xueyuse0012
发布于 2018/08/14 21:18
字数 524
阅读 2
收藏 0
%% ---
%%  Excerpted from "Programming Erlang",
%%  published by The Pragmatic Bookshelf.
%%  Copyrights apply to this code. It may not be used to create training material, 
%%  courses, books, articles, and the like. Contact us if you are in doubt.
%%  We make no guarantees that this code is fit for any purpose. 
%%  Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
%%---
-module(id3_v1).
-import(lists, [filter/2, map/2, reverse/1]).
-export([test/0, dir/1, read_id3_tag/1]).

test() -> dir("/home/joe/music_keep").

dir(Dir) ->
    Files = lib_find:files(Dir, "*.mp3", true),
    L1 = map(fun(I) -> 
		     {I, (catch read_id3_tag(I))}
	    end,  Files),
    %% L1 = [{File, Parse}] where Parse = error | [{Tag,Val}]
    %% we now have to remove all the entries from L where
    %% Parse = error. We can do this with a filter operation
    L2 = filter(fun({_,error}) -> false;
		   (_) -> true
		end, L1),
    lib_misc:dump("mp3data", L2).

read_id3_tag(File) ->
    case file:open(File, [read,binary,raw]) of
        {ok, S} ->
	    Size = filelib:file_size(File),
	    {ok, B2} = file:pread(S, Size-128, 128),
	    Result = parse_v1_tag(B2),
	    file:close(S),
	    Result;
        _Error ->
            error
    end.

parse_v1_tag(<<$T,$A,$G,
	      Title:30/binary, Artist:30/binary,
	      Album:30/binary, _Year:4/binary,
	      _Comment:28/binary, 0:8,Track:8,_Genre:8>>) ->
    {"ID3v1.1", 
     [{track,Track}, {title,trim(Title)},
      {artist,trim(Artist)}, {album, trim(Album)}]};
parse_v1_tag(<<$T,$A,$G,
	      Title:30/binary, Artist:30/binary,
	      Album:30/binary, _Year:4/binary,
	      _Comment:30/binary,_Genre:8>>) ->
    {"ID3v1", 
     [{title,trim(Title)}, 
      {artist,trim(Artist)}, {album, trim(Album)}]};
parse_v1_tag(_) ->
    error.

trim(Bin) -> 
    list_to_binary(trim_blanks(binary_to_list(Bin))).

trim_blanks(X) -> reverse(skip_blanks_and_zero(reverse(X))).

skip_blanks_and_zero([$\s|T]) -> skip_blanks_and_zero(T);
skip_blanks_and_zero([0|T])   -> skip_blanks_and_zero(T);
skip_blanks_and_zero(X)       -> X.

%% ---
%%  Excerpted from "Programming Erlang",
%%  published by The Pragmatic Bookshelf.
%%  Copyrights apply to this code. It may not be used to create training material, 
%%  courses, books, articles, and the like. Contact us if you are in doubt.
%%  We make no guarantees that this code is fit for any purpose. 
%%  Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
%%---
-module(lib_find).
-export([files/3, files/5]).
-import(lists, [reverse/1]).

-include_lib("kernel/include/file.hrl").

files(Dir, Re, Flag) -> 
    Re1 = regexp:sh_to_awk(Re),
    reverse(files(Dir, Re1, Flag, fun(File, Acc) ->[File|Acc] end, [])).

files(Dir, Reg, Recursive, Fun, Acc) ->
    case file:list_dir(Dir) of
	{ok, Files} -> find_files(Files, Dir, Reg, Recursive, Fun, Acc);
	{error, _}  -> Acc
    end.

find_files([File|T], Dir, Reg, Recursive, Fun, Acc0) ->
    FullName = filename:join([Dir,File]),
    case file_type(FullName) of
	regular ->
	    case regexp:match(FullName, Reg) of
		{match, _, _}  -> 
		    Acc = Fun(FullName, Acc0),
		    find_files(T, Dir, Reg, Recursive, Fun, Acc);
		_ ->
		    find_files(T, Dir, Reg, Recursive, Fun, Acc0)
	    end;
	directory -> 
	    case Recursive of
		true ->
		    Acc1 = files(FullName, Reg, Recursive, Fun, Acc0),
		    find_files(T, Dir, Reg, Recursive, Fun, Acc1);
		false ->
		    find_files(T, Dir, Reg, Recursive, Fun, Acc0)
	    end;
	error -> 
	    find_files(T, Dir, Reg, Recursive, Fun, Acc0)
    end;
find_files([], _, _, _, _, A) ->
    A.

file_type(File) ->
    case file:read_file_info(File) of
	{ok, Facts} ->
	    case Facts#file_info.type of
		regular   -> regular;
		directory -> directory;
		_         -> error
	    end;
	_ ->
	    error
    end.

© 著作权归作者所有

共有 人打赏支持
x
粉丝 0
博文 5
码字总数 2947
作品 0
成都
私信 提问
Java ID3 Tab Library

这个包用来读取歌曲的信息比如:从MP3文件读取歌曲的标题,艺术家,唱片套.它支持ID3v1, ID3v1.1, Lyrics3v1, Lyrics3v2, ID3v2.2, ID3v2.3,与ID3v2.4 tags....

匿名
2008/09/11
1K
0
aTunes 1.13.0 RC 发布

aTunes是一个采用Java5开发,功能丰富的音频播放器及音频文件管理器。它能够读取Ogg,ID3v1,ID3v1.1和ID3v2 tags 新版本增加了对热键的配置以及新的导入导出处理方法;采用了基于模式的标签...

红薯
2009/05/03
132
0
aTunes 3.1.0 发布,音乐播放和管理器

aTunes 3.1.0 支持任意文件的元数据存储,新的选项用于显示导航表过滤器,新的选项用于隐藏导航树。支持存储评分到文件和外部数据库,更快的 podcast 下载等等。 aTunes是一个采用Java5开发,...

oschina
2013/06/04
1K
4
aTunes 3.0.9 发布,音乐播放和管理器

aTunes 3.0.9 修复了 OS X 下的下拉框选择失败以及外观的问题修复,默认设置播放列表保存的名称。 aTunes是一个采用Java5开发,功能丰富的音频播放器及音频文件管理器。它能够读取Ogg,ID3v1...

oschina
2013/04/29
776
1
aTunes 2.1.0 发布,音频播放器

aTunes 2.1.0 发布,该版本进行了性能改进、可视化改进、bug修复和对MacOS X支持。 aTunes是一个采用Java5开发,功能丰富的音频播放器及音频文件管理器。它能够读取Ogg,ID3v1,ID3v1.1和ID3...

小卒过河
2011/10/01
327
0

没有更多内容

加载失败,请刷新页面

加载更多

activiti6+springboot连接mysql

application.properties spring.datasource.url=jdbc:mysql://130.51.23.249:3306/activiti6ui?characterEncoding=utf8&useSSL=truespring.datasource.username=saspring.datasource.passw......

Danni3
30分钟前
1
0
PHP教程中验证正整数is_int($value+0),为什么要这样?

最近学习PHP应用,其中有一段是要验证变量是否为正整数,除了is_numeric($value)外,还要加上is_int($value+0)且($value+0) > 0,为什么还要 +0呢?直接验证$value不行吗? ,只要 is_int($...

dragon_tech
41分钟前
1
0
毕业季,我的Linux求职之路

秋招终于告一段落了,本硕的七年求学之路也快画上了句号。回首求职的这一段日子,痛苦并快乐着。感谢所有陪伴着我走过这一段路程的同学,所有的辛酸都值得铭记。求职的过程中在网上看了很多的...

xiangyunyan
48分钟前
1
0
springMvc异常处理定制化

1、扩展SimpleMappingExceptionResolver来实现springMvc的定制 import org.apache.commons.codec.binary.Base64;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.......

重城重楼
今天
4
0
OSChina 周四乱弹 —— Im fine

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @LuckyXu:分享戴荃的单曲《小荃拳之歌》: 手机党少年们想听歌,请使劲儿戳(这里) 今天遇到倒霉事了, @ FalconChen :电梯宕机了我靠 但是...

小小编辑
今天
1K
20

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部