Changeset 9
- Timestamp:
- Mar 28, 2022, 6:00:50 PM (3 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.pas
r8 r9 44 44 Playlist.SearchBaseDir := ParamStr(1); 45 45 Playlist.RemoteBaseDir := ParamStr(2); 46 Playlist.Search; 46 47 Playlist.GenerateXSPF; 47 Close;48 Application.Terminate; 48 49 end; 49 50 end; -
trunk/UPlaylist.pas
r8 r9 10 10 type 11 11 TPlaylistItem = class 12 13 end; 12 Time: TDateTime; 13 Title: string; 14 RemoteName: string; 15 LocationOriginal: string; 16 Location: string; 17 Creator: string; 18 TrackNum: string; 19 Album: string; 20 end; 21 22 { TPlaylistItems } 14 23 15 24 TPlaylistItems = class(TObjectList<TPlayListItem>) 25 function AddNew: TPlaylistItem; 16 26 end; 17 27 … … 20 30 TPlaylist = class 21 31 public 32 Items: TPlaylistItems; 22 33 SearchBaseDir: string; 23 34 RemoteBaseDir: string; 24 35 FilteredFileExtensions: array of string; 36 procedure Search; 25 37 procedure GenerateM3U; 26 38 procedure GenerateXSPF; 27 39 constructor Create; 40 destructor Destroy; override; 28 41 end; 29 42 … … 50 63 begin 51 64 Path := IncludeTrailingPathDelimiter(PathName); 52 //WriteLn('Dir: ' + PathName);53 65 if FindFirst(Path + '*', faAnyFile, Rec) = 0 then 54 66 try 55 67 repeat 56 //WriteLn(Rec.Name);57 68 if Rec.Name = '..' then continue 58 69 else if Rec.Name = '.' then continue 59 70 else if (Rec.Attr and faDirectory) > 0 then FileSearch(Files, Path + Rec.Name, ExtList) 60 71 else if InStrArray(LowerCase(ExtractFileExt(Rec.Name)), ExtList) then begin 61 //WriteLn('File: ' + Rec.Name);62 72 Files.Add(Path + Rec.Name); 63 73 end; 64 74 until FindNext(Rec) <> 0; 65 75 finally 66 FindClose(Rec) ; 67 end; 68 end; 69 70 procedure TPlaylist.GenerateM3U; 71 var 72 Files: TStringList; 73 I: Integer; 74 begin 75 Files := TStringList.Create; 76 WriteLn('#EXTM3U'); 77 FileSearch(Files, SearchBaseDir, FilteredFileExtensions); 78 for I := 0 to Files.Count - 1 do 79 WriteLn(Files[I]); 80 Files.Free; 76 FindClose(Rec); 77 end; 81 78 end; 82 79 … … 92 89 end; 93 90 91 92 { TPlaylistItems } 93 94 function TPlaylistItems.AddNew: TPlaylistItem; 95 begin 96 Result := TPlaylistItem.Create; 97 Add(Result); 98 end; 99 100 procedure TPlaylist.Search; 101 var 102 Files: TStringList; 103 I: Integer; 104 PlaylistItem: TPlaylistItem; 105 NameRemoved: Boolean; 106 Part: string; 107 OutInt: Integer; 108 begin 109 Files := TStringList.Create; 110 try 111 FileSearch(Files, SearchBaseDir, FilteredFileExtensions); 112 for I := 0 to Files.Count - 1 do begin 113 PlaylistItem := Items.AddNew; 114 with PlayListItem do begin 115 RemoteName := RemoteBaseDir + Copy(Files[I], Length(ParamStr(1)) + 2, High(Integer)); 116 LocationOriginal := StringReplace(RemoteName, '&', '&', [rfReplaceAll]); 117 Location := StringReplace(LocationOriginal, '_', ' ', [rfReplaceAll]); 118 Album := ExtractFileName(ExtractFileDir(Location)); 119 NameRemoved := False; 120 121 // Detect Creator from directory name 122 if Pos(' - ', Album) > 0 then begin 123 Creator := Copy(Album, 1, Pos(' - ', Album) - 1); 124 Delete(Album, 1, Length(Creator) + 3); 125 end else Creator := Album; 126 Title := Copy(ExtractFileName(Location), 1, Length(ExtractFileName(Location)) - Length(ExtractFileExt(Location))); 127 128 // Remove starting with album name from title 129 Part := Creator + ' - '; 130 if Copy(Title, 1, Length(Part)) = Part then begin 131 Delete(Title, 1, Length(Part)); 132 NameRemoved := True; 133 end; 134 135 // Try to load Creator from name 136 if (Pos(' - ', Title) > 0) and (not NameRemoved) then begin 137 Part := Copy(Title, 1, Pos(' - ', Title) - 1); 138 // Avoid track number which can be first title part 139 if not TryStrToInt(Part, OutInt) then begin 140 Creator := Part; 141 Delete(Title, 1, Length(Creator) + 3); 142 end; 143 end; 144 // Detect track number from title 145 if Pos(' - ', Title) > 0 then begin 146 TrackNum := Copy(Title, 1, Pos(' - ', Title) - 1); 147 if TryStrToInt(TrackNum, OutInt) then 148 Delete(Title, 1, Length(TrackNum) + 3) 149 else TrackNum := ''; 150 end else TrackNum := ''; 151 152 Time := FileDate(Files[I]); 153 end; 154 end; 155 finally 156 Files.Free; 157 end; 158 end; 159 160 procedure TPlaylist.GenerateM3U; 161 var 162 I: Integer; 163 begin 164 WriteLn('#EXTM3U'); 165 for I := 0 to Items.Count - 1 do 166 with Items[I] do begin 167 WriteLn(LocationOriginal); 168 end; 169 end; 170 94 171 procedure TPlaylist.GenerateXSPF; 95 172 var 96 Files: TStringList; 97 I: Integer; 98 Creator: string; 99 Album: string; 100 Location: string; 101 LocationOriginal: string; 102 Title: string; 103 Part: string; 104 TrackNum: string; 105 Out: Integer; 106 NameRemoved: Boolean; 107 RemoteName: string; 108 begin 109 Files := TStringList.Create; 173 I: Integer; 174 begin 110 175 WriteLn('<?xml version="1.0" encoding="UTF-8"?>'); 111 176 WriteLn('<playlist version="1" xmlns="http://xspf.org/ns/0/">'); 112 177 WriteLn('<trackList>'); 113 FileSearch(Files, SearchBaseDir, FilteredFileExtensions); 114 for I := 0 to Files.Count - 1 do begin 115 RemoteName := RemoteBaseDir + Copy(Files[I], Length(ParamStr(1)) + 2, High(Integer)); 116 LocationOriginal := StringReplace(RemoteName, '&', '&', [rfReplaceAll]); 117 Location := StringReplace(LocationOriginal, '_', ' ', [rfReplaceAll]); 118 Album := ExtractFileName(ExtractFileDir(Location)); 119 NameRemoved := False; 120 121 // Detect Creator from directory name 122 if Pos(' - ', Album) > 0 then begin 123 Creator := Copy(Album, 1, Pos(' - ', Album) - 1); 124 Delete(Album, 1, Length(Creator) + 3); 125 end else Creator := Album; 126 Title := Copy(ExtractFileName(Location), 1, Length(ExtractFileName(Location)) - Length(ExtractFileExt(Location))); 127 128 // Remove starting with album name from title 129 Part := Creator + ' - '; 130 if Copy(Title, 1, Length(Part)) = Part then begin 131 Delete(Title, 1, Length(Part)); 132 NameRemoved := True; 133 end; 134 135 // Try to load Creator from name 136 if (Pos(' - ', Title) > 0) and (not NameRemoved) then begin 137 Part := Copy(Title, 1, Pos(' - ', Title) - 1); 138 // Avoid track number which can be first title part 139 if not TryStrToInt(Part, Out) then begin 140 Creator := Part; 141 Delete(Title, 1, Length(Creator) + 3); 142 end; 143 end; 144 // Detect track number from title 145 if Pos(' - ', Title) > 0 then begin 146 TrackNum := Copy(Title, 1, Pos(' - ', Title) - 1); 147 if TryStrToInt(TrackNum, Out) then 148 Delete(Title, 1, Length(TrackNum) + 3) 149 else TrackNum := ''; 150 end else TrackNum := ''; 151 178 for I := 0 to Items.Count - 1 do 179 with Items[I] do begin 152 180 WriteLn('<track>'); 153 181 WriteLn('<title>' + Title + '</title>'); … … 156 184 WriteLn('<creator>' + Creator + '</creator>'); 157 185 WriteLn('<trackNum>' + TrackNum + '</trackNum>'); 158 WriteLn('<annotation>' + FormatDateTime('yyyy-mm-dd', FileDate(Files[I])) + '</annotation>');186 WriteLn('<annotation>' + FormatDateTime('yyyy-mm-dd', Time) + '</annotation>'); 159 187 WriteLn('</track>'); 160 188 end; 161 189 WriteLn('</trackList>'); 162 190 WriteLn('</playlist>'); 163 Files.Free;164 191 end; 165 192 … … 167 194 begin 168 195 FilteredFileExtensions := ['.mp3', '.ac3', '.flac', '.it', '.m4a', '.wma', '.s3m', '.ogg']; 196 Items := TPlaylistItems.Create; 197 end; 198 199 destructor TPlaylist.Destroy; 200 begin 201 FreeAndNil(Items); 202 inherited; 169 203 end; 170 204
Note:
See TracChangeset
for help on using the changeset viewer.