001/* 002 * 003 * $Revision: 1295 $ 004 * $Date: 2019-04-18 18:05:01 -0700 (Thu, 18 Apr 2019) $ 005 * $Locker: $ 006 * 007 * 008 * Written by Tom Gutwin - WebARTS Design. 009 * Copyright (C) 2010-2012 WebARTS Design, North Vancouver Canada 010 * http://www.webarts.bc.ca 011 * 012 * This program is free software; you can redistribute it and/or modify 013 * it under the terms of the GNU General Public License as published by 014 * the Free Software Foundation; either version 2 of the License, or 015 * (at your option) any later version. 016 * 017 * This program is distributed in the hope that it will be useful, 018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 020 * GNU General Public License for more details. 021 * 022 * You should have received a copy of the GNU General Public License 023 * along with this program; if not, write to the Free Software 024 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 025 */ 026package ca.bc.webarts.tools; 027 028import ca.bc.webarts.widgets.Util; 029import ca.bc.webarts.tools.JOrbisComment; 030import java.io.File; 031import java.util.Vector; 032 033class RenameOggDirFiles 034{ 035 /** The users home ditrectory. */ 036 public static String USERHOME = System.getProperty("user.home"); 037 038 /** The users pwd ditrectory. */ 039 public static String USERDIR = System.getProperty("user.dir"); 040 public static boolean testing = false; 041 public static boolean doSync = false; 042 public static boolean verboseOut = false; 043 044 /** The file extension (type) to load **/ 045 public static String FILETYPE = "ogg"; 046 public static String fileType = FILETYPE; 047 048 public static Vector<File> songDuds = new Vector(); 049 public static short numArt = 0; 050 public static short numAlb = 0; 051 public static short numSongs = 0; 052 053 054 /** Main **/ 055 public static void main (String [] args) 056 { 057 boolean topDirIsArtistDir = false; 058 059 if (args.length >0) 060 { 061 // first make sure any spaces are converted 062 Util.spacesToCapsInDir(args[args.length-1], false, true); // dirName, doUnderscores, verboseOut 063 064 if (args.length >1 && args[0].equals("-testing")) testing = true; 065 if (args.length >1 && args[0].equals("-sync")) doSync = true; 066 if (args.length >1 && args[0].equals("-type")) fileType = args[1]; 067 068 if (args.length >2 && args[1].equals("-testing")) testing = true; 069 if (args.length >2 && args[1].equals("-sync")) doSync = true; 070 if (args.length >2 && args[1].equals("-type")) fileType = args[2]; 071 072 if (testing) System.out.println(" TESTING ONLY "); 073 if (doSync) System.out.println(" Syncing comments "); 074 075 // now go back and add the artist to the ogg filename 076 // go through all the artists dirs inside the passed topDir 077 String topDirFilename = args[args.length-1]; 078 File topDirFile = new File(topDirFilename); 079 String [] topArtistDirfilenames = topDirFile.list(); 080 if (topDirFile != null && 081 topDirFile.isDirectory() && 082 topDirFile.canRead() ) 083 { 084 try 085 { 086 // Process this dir in case the user gave an artist dir to start 087 doArtistDir(Util.spacesToCapsInString(topDirFilename)); 088 for (int i=0;i< topArtistDirfilenames.length;i++) 089 { 090 // Do Each Artists Directory 091 doArtistDir(topDirFilename+File.separator+topArtistDirfilenames[i]); 092 093 } // topArtistDirfiles loop 094 } 095 catch (Exception ex) 096 { 097 ex.printStackTrace(); 098 } 099 } 100 if (verboseOut) System.out.println("\nSongs with suspect sizes:"+songDuds.size()); 101 102 for (int i = 0; i < songDuds.size(); i++) 103 { 104 System.out.println(((File)songDuds.get(i)).getAbsolutePath() + " " + ((File)songDuds.get(i)).length() ); 105 } 106 } 107 else 108 { 109 System.out.println(" You must supply the directory on the commandline"); 110 System.out.println(" The dir should be the the Artist dir so it can parse the sub-album dirs."); 111 System.out.println(" a second argument can be specified "); 112 System.out.println(" - if second argument is '-testing' to allow no actual writes"); 113 System.out.println(" - if second argument is '-sync' it will sync the comments with the file/dir"); 114 System.out.println(" - if second argument is '-type' it will use the next arg as the file extension"); 115 if (args.length >1) System.out.println(" arg1=" + args[1]); 116 if (args.length >2) System.out.println(" arg2=" + args[2]); 117 if (args.length >3) System.out.println(" arg3=" + args[3]); 118 } 119 } 120 121 122 private static void doArtistDir(String dirName) 123 { 124 JOrbisComment vorbisInfo = null; 125 String oggArt, oggAl, oggTitle, oggTrack; // copy of ogg file comments 126 File artistDirFile = new File(dirName); 127 if (artistDirFile != null && artistDirFile.isDirectory()) 128 { 129 // Drill into each album dir 130 numArt++; 131 132 String artistDirName = artistDirFile.getName(); 133 if (verboseOut) System.out.println(" parsing artistDir"+numArt+"="+artistDirName); 134 String [] albumDirFilenames = artistDirFile.list(); 135 boolean successfull = false; 136 for (int j=0;j< albumDirFilenames.length;j++) 137 { 138 numAlb++; 139 // Do Each Album Directory 140 //if (verboseOut) System.out.println(" albumDir"+numAlb+"="+dirName+File.separator+albumDirFilenames[j]); 141 File albumDirFiles = new File(dirName+File.separator+albumDirFilenames[j]); 142 if (albumDirFiles != null && albumDirFiles.isDirectory()) 143 { 144 String [] albumSongFilenames = albumDirFiles.list(); 145 if (verboseOut) System.out.print(" album="+albumDirFilenames[j]); 146 if (verboseOut) System.out.println(" ("+albumSongFilenames.length+")"); 147 for (int k=0;k< albumSongFilenames.length; k++) 148 { 149 // Do Each Song in album dirpacesToCaps 150 if (albumSongFilenames[k].endsWith(fileType)) 151 { 152 numSongs++; 153 //System.out.print("."); 154 File songFile = new File(dirName+File.separator+albumDirFilenames[j]+File.separator+albumSongFilenames[k]); 155 vorbisInfo = new JOrbisComment(songFile); 156 oggAl = vorbisInfo.getAlbum(); 157 oggAl = Util.tokenReplace(oggAl,"(explicit)",""); 158 oggAl = Util.tokenReplace(oggAl,"explicit",""); 159 oggAl = Util.tokenReplace(oggAl,"(Explicit)",""); 160 oggAl = Util.tokenReplace(oggAl,"Explicit",""); 161 oggAl = Util.tokenReplace(oggAl,"(EXPLICIT)",""); 162 oggAl = Util.tokenReplace(oggAl,"EXPLICIT",""); 163 oggArt = vorbisInfo.getArtist(); 164 oggTitle = vorbisInfo.getTitle(); 165 oggTitle = Util.tokenReplace(oggTitle,"(explicit)",""); 166 oggTitle = Util.tokenReplace(oggTitle,"explicit",""); 167 oggTitle = Util.tokenReplace(oggTitle,"(Explicit)",""); 168 oggTitle = Util.tokenReplace(oggTitle,"Explicit",""); 169 oggTitle = Util.tokenReplace(oggTitle,"(EXPLICIT)",""); 170 oggTitle = Util.tokenReplace(oggTitle,"EXPLICIT",""); 171 oggTrack = vorbisInfo.getTracknumber(); 172 //songFile.close(); 173 174 if(oggArt != null ) 175 oggArt = Util.tokenReplace(Util.tokenReplace(oggArt,"\\"," "),"/"," "); 176 else 177 oggArt = ""; 178 179 if(oggTitle != null ) 180 oggTitle = Util.tokenReplace(Util.tokenReplace(oggTitle,"\\"," "),"/"," "); 181 else 182 oggTitle = ""; 183 184 if(oggAl != null ) 185 oggAl = Util.tokenReplace(Util.tokenReplace(oggAl,"\\"," "),"/"," "); 186 else 187 oggAl = ""; 188 189 if(oggTrack != null ) 190 oggTrack = Util.tokenReplace(Util.tokenReplace(oggTrack,"\\"," "),"/"," "); 191 else 192 oggTrack = ""; 193 194 //if (verboseOut) System.out.print(" oggART="+oggART +", oggArt="+oggArt); 195 //if (verboseOut) System.out.print(" oggAL="+oggAL +", oggAl="+oggAl); 196 //if (verboseOut) System.out.print(" oggTITLE="+oggTITLE +", oggTitle="+oggTitle); 197 //if (verboseOut) System.out.println(" oggTRACK="+oggTRACK +", oggTrack="+oggTrack); 198 199 // String trackTitle = cleanedTrackTitle(albumSongFilenames[k], artistDirName); 200 //if (verboseOut) System.out.print("."); 201 //String newFilename = artistDirName+"_"+oggTrack+"-"+trackTitle+"."+fileType; 202 if (oggArt.length()>0 && oggTitle.length()>0) 203 { 204 String newFilename = Util.spacesToCapsInString(oggArt, true)+"_"+ 205 (oggTrack.length()==1?"0":"")+ 206 Util.spacesToCapsInString(oggTrack, true)+"-"+ 207 Util.spacesToCapsInString(oggTitle, true)+"."+fileType; 208 File newFile = new File(dirName+File.separator+albumDirFilenames[j]+File.separator+newFilename); 209 successfull = false; 210 if(!testing) 211 { 212 // 1st check if the names are already corrected 213 if (!newFilename.equals(albumSongFilenames[k])) 214 { 215 if (verboseOut) System.out.print("Renaming "+numArt+","+numAlb+","+numSongs+" "+ 216 dirName+File.separator+albumDirFilenames[j]+File.separator+albumSongFilenames[k]); 217 if (verboseOut) System.out.print(" --> "+dirName+File.separator+albumDirFilenames[j]+File.separator+newFilename); 218 successfull = songFile.renameTo(newFile); 219 if (successfull) 220 { 221 if (verboseOut) 222 System.out.println(" successfully"); 223 else 224 System.out.print("."); 225 if (newFile.length() < 10000) songDuds.add( newFile); 226 } 227 else 228 { 229 if (verboseOut) 230 System.out.println(" UN-successfully"); 231 else 232 System.out.print("."); 233 if (songFile.length() < 10000) songDuds.add( songFile); 234 } 235 } 236 } 237 else if (songFile.length() < 10000) songDuds.add( songFile); 238 // Pass the dirnames and Title to the Sync method to make sure they are commented. 239 if(!testing && doSync) syncOggComments(newFile, /* artist dirname */ artistDirName, /* album dirname */albumDirFilenames[j], newFilename ); 240 } 241 else if (songFile.length() < 10000) songDuds.add( songFile); 242 } 243 } 244 } 245 } 246 } 247 } 248 249 250 /** take a song filename and clean off the artist, and extrra junk **/ 251 private static String cleanedTrackTitle(String dirty, String art) 252 { 253 String clean= dirty; 254 //String art = ""; 255 //if (verboseOut) System.out.println("\n1 "+clean); 256 257 // remove double underscores 258 Util.tokenReplace(clean,"__", "_"); 259 260 if (clean.endsWith("."+fileType)) clean = clean.substring(0, clean.length()-4).trim(); 261 //if (verboseOut) System.out.println("2 "+clean); 262 if (clean.indexOf(art)==0) clean = clean.substring(art.length()).trim(); 263 //if (verboseOut) System.out.println("3 "+clean); 264 265 // remove double underscores 266 Util.tokenReplace(clean,"__", ""); 267 Util.tokenReplace(clean,"_", ""); 268 //if (verboseOut) System.out.println("4 "+clean); 269 270 // remove multiple artistnames 271 if (clean.toLowerCase().indexOf(art.toLowerCase()) != -1) 272 { 273 //art = clean.substring(0,clean.indexOf("_")); 274 clean = clean.substring(clean.toLowerCase().lastIndexOf(art.toLowerCase())+art.length()).trim(); 275 //if (verboseOut) System.out.println("5 "+clean); 276 } 277 278 // remove Artist Name 279 Util.tokenReplace(clean,art, ""); 280 //if (verboseOut) System.out.println("6 "+clean); 281 282 clean = Util.spacesToCapsInString(clean, true); 283 284 //if (verboseOut) System.out.println("7 "+clean); 285 if (clean.indexOf(art) != -1) 286 { 287 clean = clean.substring(clean.lastIndexOf(art)+art.length()).trim(); 288 //if (verboseOut) System.out.println("8 "+clean); 289 } 290 291 return clean; 292 } 293 294 295 /** a method to add vorbis comments if they don't exist. It uses the passed in values. **/ 296 public static boolean syncOggComments(File oggFile, String art,String al,String name) 297 { 298 boolean retVal = true; 299 JOrbisComment vorbisInfo = null; 300 String oggArt, oggAl, oggTitle; // copy of ogg file comments 301 boolean valuesChanged = false; 302 303 try 304 { 305 vorbisInfo = new JOrbisComment(oggFile); 306 oggAl = vorbisInfo.getAlbum(); 307 oggArt = vorbisInfo.getArtist(); 308 oggTitle = vorbisInfo.getTitle(); 309 //if (verboseOut) System.out.println(" oggComments="+vorbisInfo.toString()); 310 if (verboseOut) System.out.print(" oggArtist="+oggArt); 311 if (verboseOut) System.out.print(" oggAlbum="+oggAl); 312 if (verboseOut) System.out.println(" renameoggtitle="+oggTitle); 313 String oggAL = vorbisInfo.getComment("ALBUM"); 314 String oggART = vorbisInfo.getComment("ARTIST"); 315 String oggTITLE = vorbisInfo.getComment("TITLE"); 316 317 if (oggArt ==null || oggArt.equals("")) 318 { 319 if (verboseOut) System.out.println("Setting artistComment: "+ 320 Util.capsToSpacesInString(art).trim()); 321 vorbisInfo.setComment("artist", 322 Util.capsToSpacesInString(art).trim()); 323 valuesChanged = true; 324 } 325 if (oggAl ==null || oggAl.equals("")) 326 { 327 if (verboseOut) System.out.println("Setting albumComment: "+ 328 Util.capsToSpacesInString(al).trim()); 329 vorbisInfo.setComment("album", 330 Util.capsToSpacesInString(al).trim()); 331 valuesChanged = true; 332 } 333 if (oggTitle ==null || oggTitle.equals("")) 334 { 335 if (verboseOut) System.out.println("Setting nameComment: "+ 336 Util.capsToSpacesInString(name).trim()); 337 vorbisInfo.setComment("title", 338 Util.capsToSpacesInString(name).trim()); 339 valuesChanged = true; 340 } 341 342 if (oggART ==null || oggART.equals("")) 343 { 344 if (verboseOut) System.out.println("Setting artistComment: "+ 345 Util.capsToSpacesInString(art).trim()); 346 vorbisInfo.setComment("ARTIST", 347 Util.capsToSpacesInString(art).trim()); 348 valuesChanged = true; 349 } 350 if (oggAL ==null || oggAL.equals("")) 351 { 352 if (verboseOut) System.out.println("Setting albumComment: "+ 353 Util.capsToSpacesInString(al).trim()); 354 vorbisInfo.setComment("ALBUM", 355 Util.capsToSpacesInString(al).trim()); 356 valuesChanged = true; 357 } 358 if (oggTITLE ==null || oggTITLE.equals("")) 359 { 360 if (verboseOut) System.out.println("Setting nameComment: "+ 361 Util.capsToSpacesInString(name).trim()); 362 vorbisInfo.setComment("TITLE", 363 Util.capsToSpacesInString(name).trim()); 364 valuesChanged = true; 365 } 366 367 if (valuesChanged) vorbisInfo.write(); 368 } 369 catch (Exception ex) 370 { 371 System.out.println("can't get oggComments for "+ oggFile.toString()); 372 ex.printStackTrace(); 373 retVal = false; 374 } 375 376 377 return retVal; 378 } 379 380}