jaudiotagger获取MP3文件信息及封面图片

#jaudiotagger获取MP3文件信息及封面图片
###maven依赖:

<dependency>
    <groupId>org</groupId>
    <artifactId>jaudiotagger</artifactId>
    <version>2.0.3</version>
</dependency>

###java代码:

  • MP3信息对象(自定义):
public class MP3Info {
    /**
     * 歌曲名字
     */
    private String songName;

    /**
     * 歌手名字
     */
    private String singer;

    /**
     * 时长(秒)
     */
    private Integer duration;

    public String getSongName() {
        return songName;
    }

    public void setSongName(String songName) {
        this.songName = songName;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    public Integer getDuration() {
        return duration;
    }

    /**
     * 返回hh:MM:ss形式
     * @return
     */
    public String getDurationFormat() {
        if(duration/60 >= 60) {
            return String.format("%02d",duration/3600) + String.format("%02d",(duration%3600)/60) + ":" + String.format("%02d",duration%60);
        } else {
            return "00:" + String.format("%02d",duration/60) + ":" + String.format("%02d",duration%60);
        }
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }
}

  • MP3工具类:
import com.springapp.util.domain.MP3Info;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Mp3Util {

    /**
     * 获取MP3歌曲名、歌手、时长信息
     * @param mp3File
     * @return
     */
    public static MP3Info getMP3Info(File mp3File) {
        MP3Info mp3Info = new MP3Info();
        try {
            MP3File file = new MP3File(mp3File);
            mp3Info.setSongName(toGB2312(file.getID3v2Tag().frameMap.get("TIT2").toString()));
            mp3Info.setSinger(toGB2312(file.getID3v2Tag().frameMap.get("TPE1").toString()));
            MP3AudioHeader audioHeader = (MP3AudioHeader)file.getAudioHeader();
            mp3Info.setDuration(audioHeader.getTrackLength());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TagException e) {
            e.printStackTrace();
        } catch (ReadOnlyFileException e) {
            e.printStackTrace();
        } catch (InvalidAudioFrameException e) {
            e.printStackTrace();
        }
        return mp3Info;
    }


    /**
     * 获取MP3封面图片
     * @param mp3File
     * @return
     */
    public static byte[] getMP3Image(File mp3File) {
        byte[] imageData = null;
        try {
            MP3File mp3file = new MP3File(mp3File);
            AbstractID3v2Tag tag = mp3file.getID3v2Tag();
            AbstractID3v2Frame frame = (AbstractID3v2Frame) tag.getFrame("APIC");
            FrameBodyAPIC body = (FrameBodyAPIC) frame.getBody();
            imageData = body.getImageData();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return imageData;
    }



    /**
     *获取mp3图片并将其保存至指定路径下
     * @param mp3File mp3文件对象
     * @param mp3ImageSavePath mp3图片保存位置(默认mp3ImageSavePath +"\" mp3File文件名 +".jpg" )
     * @param cover 是否覆盖已有图片
     * @return 生成图片全路径
     */
    public static String saveMP3Image(File mp3File, String mp3ImageSavePath, boolean cover) {
        //生成mp3图片路径
        String mp3FileLabel = getFileLabel(mp3File.getName());
        String mp3ImageFullPath = mp3ImageSavePath + ("\\" + mp3FileLabel + ".jpg");

        //若为非覆盖模式,图片存在则直接返回(不再创建)
        if( !cover ) {
            File tempFile = new File(mp3ImageFullPath) ;
            if(tempFile.exists()) {
                return mp3ImageFullPath;
            }
        }

        //生成mp3存放目录
        File saveDirectory = new File(mp3ImageSavePath);
        saveDirectory.mkdirs();

        //获取mp3图片
        byte imageData[] = getMP3Image(mp3File);
        //若图片不存在,则直接返回null
        if (null == imageData || imageData.length == 0) {
            return null;
        }
        //保存mp3图片文件
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(mp3ImageFullPath);
            fos.write(imageData);
            fos.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return mp3ImageFullPath;
    }


    /**
     * 仅返回文件名(不包含.类型)
     * @param fileName
     * @return
     */
    private static String getFileLabel(String fileName) {
        int indexOfDot = fileName.lastIndexOf(".");
        fileName = fileName.substring(0,(indexOfDot==-1?fileName.length():indexOfDot));
        return fileName;
    }
    private static String toGB2312(String s) {
        try {
            return new String(s.getBytes("ISO-8859-1"), "gb2312");
        } catch (Exception e) {
            return s;
        }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗小爬EX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值