본문 바로가기
Developer/Java

[Java] 음악파일 정보 추출

by 순수한소년 2022. 9. 25.
728x90
반응형

@

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package test;
 
import java.io.File;
import java.io.IOException;
 
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
 
public class test2 {
 
    public static void main(String[] args) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
        
        String strOrgDir = "C:\\Users\\tiger\\Downloads\\01 New Music Try Convert\\";
        String strCvtDir = "C:\\Users\\tiger\\Downloads\\02 Completion\\";
        File dir = new File(strOrgDir);
        /*
        디렉토리 안 파일의 패턴
        기존 : Allan Berndtz - Lost Paradise.mp3
        변경 : 2008 - Allan Berndtz - Lost Paradise.mp3
        */
        File files[] = dir.listFiles();
        int cnt = 1;
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
//                System.out.println(file.getPath());
                
            } else {
                
//                System.out.println("["+cnt+"]"+file);
                String path ="";
                String fileName ="";
                
                
                path = file.getParentFile().toString();
                fileName = file.getName();
                
                int index = fileName.lastIndexOf(".");
                
                // 확장자 추출
//                if (index > 0) {
                // 파일이름에서 '.' 이후의 문자열이 확장자가 된다.
                String orgExtension = fileName.substring(index + 1);
//                }
 
                String[] tempEmail = fileName.split(" - ");
                String orgArtist = null;
                String orgTitleRdA = null;
                String[] orgTitleRdB = null;
                String orgTitle = null;
                
                if (tempEmail != null && tempEmail.length >= 2) {
                   orgArtist = tempEmail[0];
                   orgTitleRdA = tempEmail[1];
                   orgTitleRdB = orgTitleRdA.split(".mp3");
                   orgTitle = orgTitleRdB[0];
                }
                
                if (("").equals(fileName)) {
                    System.out.println("더이상 파일이 존재하지 않습니다.");      //파일명
                    break;
                }
                System.out.println("\n1 파일명:"+fileName);       //파일명
                System.out.println("2 가수:"+orgArtist);         //가수
                System.out.println("3 제목준비A:"+orgTitleRdA);   //제목준비A
                System.out.println("4 제목준비B:"+orgTitleRdB);   //제목준비B
                System.out.println("5 파일명:"+orgTitle);        //파일명
                System.out.println("6 확장자:"+orgExtension);    //mp3
                
                if (orgArtist != null) {
                    
                        /* ################################################################################### */
                        if ( !("desktop.ini").equals(fileName)) {
                            
                            MP3File orgMP3 = (MP3File) AudioFileIO.read(file);
                            AbstractID3v2Tag tag2 = orgMP3.getID3v2Tag();
                            
                            Tag tag = orgMP3.getTag();
                            String title  = tag.getFirst(FieldKey.TITLE);
                            String artist = tag.getFirst(FieldKey.ARTIST);
                            String album  = tag.getFirst(FieldKey.ALBUM);
                            String year   = tag.getFirst(FieldKey.YEAR);
                            String genre  = tag.getFirst(FieldKey.GENRE);
                            
                            StringBuffer strBf = new StringBuffer();
                            strBf.append(strCvtDir);
                            strBf.append(year);
                            strBf.append(" - ");
                            strBf.append(orgArtist);
                            strBf.append(" - ");
                            strBf.append(orgTitle);
                            strBf.append(".");
                            strBf.append(orgExtension);
                            String strNewFileName = strBf.toString();
                            
                            StringBuffer strBf2 = new StringBuffer();
                            //                System.out.println("year : " + year);               //장르
                            //                System.out.println("orgArtist : " +orgArtist);     //가수
                            //                System.out.println("orgTitle : " +orgTitle);      //파일명
                            //                System.out.println("orgExtension : " +orgExtension);  //mp3
                            
                            strBf2.append(year);
                            strBf2.append(" - ");
                            strBf2.append(orgArtist);
                            strBf2.append(" - ");
                            strBf2.append(orgTitle);
                            String strNewFileName2 = strBf2.toString();
                            System.out.println("strNewFileName2 최종파일명: " + "\n"+ strNewFileName2);
                            
                            File newFile = new File(strNewFileName);
                            boolean result = file.renameTo(newFile);
                            //                System.out.println("result : " + result);
                            //                    System.out.println("Tag : " + tag2);
                            //                    System.out.println("Song Name : " + title);    //제목
                            //                    System.out.println("Artist : " + artist);        //아티스트
                            //                    System.out.println("Album : " + album);        //앨범
                            //                    System.out.println("Year : " + year);            //연도
                            //                    System.out.println("Genre : " + genre);        //장르
                            
                            MP3File mp3 = (MP3File) AudioFileIO.read(newFile);
                            Tag tagfinal = mp3.getTag();
                            tagfinal.setField(FieldKey.TITLE, strNewFileName2);
                            tagfinal.setField(FieldKey.ARTIST, orgArtist);
                            tagfinal.setField(FieldKey.ALBUM, orgArtist);
                            tagfinal.setField(FieldKey.YEAR, year);
                            
                            //                System.out.println("year : " + tag.getFirst(FieldKey.YEAR));
                            mp3.setTag(tagfinal);
                            mp3.save();
                            cnt++;
                        }
                        /* ################################################################################### */
                }
            }
            
        }
    }
}
cs

@

728x90
반응형