diff options
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/FileUtility.java | 14 | ||||
| -rw-r--r-- | src/main/java/Main.java | 142 | ||||
| -rw-r--r-- | src/main/java/TagEditorScreen.form | 2 | ||||
| -rw-r--r-- | src/main/java/TagEditorScreen.java | 38 |
4 files changed, 118 insertions, 78 deletions
diff --git a/src/main/java/FileUtility.java b/src/main/java/FileUtility.java index cc0dffb..73178ae 100644 --- a/src/main/java/FileUtility.java +++ b/src/main/java/FileUtility.java @@ -118,6 +118,20 @@ public class FileUtility { } } } + + public static String showImageFileChooser() { + javax.swing.JFileChooser chooser = new javax.swing.JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG Image File", "jpg", "jpeg"); + chooser.setFileFilter(filter); + chooser.setDialogTitle("Select a image file"); + chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY); + chooser.setAcceptAllFileFilterUsed(false); + if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) { + return chooser.getSelectedFile().getAbsolutePath(); + } else { + return null; + } + } public static ArrayList<String> txtToArrayList(String fileName) { ArrayList<String> lines = new ArrayList<String>(); try { diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 05edb94..c324ccd 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -22,7 +22,7 @@ public class Main extends JFrame { JProgressBar progressBar = new JProgressBar(); JLabel title = new JLabel("YouTube to MP3 Auto Tagging"); JButton startButton = new JButton("Set .txt File"); - static JTextArea outputArea = new JTextArea("this is bery bery bery safe no worries no virus malwar ur monies back granteed"); + static JTextArea outputArea = new JTextArea(""); public Main(){ initializeComponents(); @@ -33,6 +33,75 @@ public class Main extends JFrame { new Main().setVisible(true); } + private void downloadAndTag(){ //Main loop ran for checking list of songs, downloading mp3 files, and applying tags + ArrayList<String> songs = fileUtil.txtToArrayList(textPath); + progress = 0; + for(int i = 0;i<songs.size();i++) { + try { + fileUtil.deleteAllFilesDir("downloaded"); + youtubeToMP3(songs.get(i)); + String info[] = fileUtil.parseJson(fileUtil.jsonToString(fileUtil.findJsonFile("downloaded"))); //title,uploader + String uploader = info[1]; + String title = info[0]; + AudioFile f = AudioFileIO.read(fileUtil.findMP3File("downloaded")); + Tag tag = f.getTag(); + tag.setField(FieldKey.ARTIST, uploader); + tag.setField(FieldKey.TITLE, title); + fileUtil.downloadImage("https://img.youtube.com/vi/"+info[2]+"/maxresdefault.jpg","img.jpg"); + Artwork cover = Artwork.createArtworkFromFile(new File("img.jpg")); + tag.addField(cover); + f.commit(); + fileUtil.deleteFile("img.jpg"); + fileUtil.moveFile(fileUtil.findMP3File("downloaded").getAbsolutePath(), "completed/" + fileUtil.removeNonAlphaNumeric(info[0]) + " ["+info[2]+ "].mp3"); + outputArea.setText(outputArea.getText()+"\n"+"Moved file to Completed Folder"); + progress = i; + System.out.println("Current Progress " + calculatePercentage(i+1,songs.size())); + progressBar.setValue(calculatePercentage(i+1,songs.size())); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private int calculatePercentage(int current, int total){//Calculate the percentage when give numerator and denominator + double currentD = current; + double totalD = total; + return (int)((currentD/totalD)*100); + } + + public static void showWarning(String message) { + JOptionPane.showMessageDialog(null, message, "JUST YOUR FRIENDLY NEIGHBORLY REMINDER", JOptionPane.WARNING_MESSAGE); + } + + public static void youtubeToMP3(String url) {//Download mp3 of youtube video using yt-dlp.exe. Ran from cmd + try { + ProcessBuilder builder = new ProcessBuilder( + "yt-dlp.exe", + "--extract-audio", + "--audio-format", "mp3", + "--audio-quality", "0", + "--output", "downloaded/%(title)s_%(id)s.mp3", + "--ffmpeg-location","ffmpeg.exe", + "--write-info-json", + url + ); + builder.redirectErrorStream(true); + Process p = builder.start(); + BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); + String line; + while (true) { + line = r.readLine(); + if (line == null) { + break; + } + outputArea.setText(outputArea.getText()+"\n"+line); + System.out.println(line); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + private void initializeComponents(){//Initiate GUI components this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); @@ -106,76 +175,5 @@ public class Main extends JFrame { }); } - private void downloadAndTag(){ //Main loop ran for checking list of songs, downloading mp3 files, and applying tags - ArrayList<String> songs = fileUtil.txtToArrayList(textPath); - progress = 0; - for(int i = 0;i<songs.size();i++) { - try { - fileUtil.deleteAllFilesDir("downloaded"); - youtubeToMP3(songs.get(i)); - String info[] = fileUtil.parseJson(fileUtil.jsonToString(fileUtil.findJsonFile("downloaded"))); //title,uploader - String uploader = info[1]; - String title = info[0]; - AudioFile f = AudioFileIO.read(fileUtil.findMP3File("downloaded")); - Tag tag = f.getTag(); - tag.setField(FieldKey.ARTIST, uploader); - tag.setField(FieldKey.TITLE, title); - fileUtil.downloadImage("https://img.youtube.com/vi/"+info[2]+"/maxresdefault.jpg","img.jpg"); - Artwork cover = Artwork.createArtworkFromFile(new File("img.jpg")); - tag.addField(cover); - f.commit(); - fileUtil.deleteFile("img.jpg"); - fileUtil.moveFile(fileUtil.findMP3File("downloaded").getAbsolutePath(), "completed/" + fileUtil.removeNonAlphaNumeric(info[0]) + " ["+info[2]+ "].mp3"); - outputArea.setText(outputArea.getText()+"\n"+"Moved file to Completed Folder"); - progress = i; - System.out.println("Current Progress " + calculatePercentage(i+1,songs.size())); - progressBar.setValue(calculatePercentage(i+1,songs.size())); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - private int calculatePercentage(int current, int total){//Calculate the percentage when give numerator and denominator - double currentD = current; - double totalD = total; - return (int)((currentD/totalD)*100); - } - - public static void showWarning(String message) { - JOptionPane.showMessageDialog(null, message, "JUST YOUR FRIENDLY NEIGHBORLY REMINDER", JOptionPane.WARNING_MESSAGE); - } - - public static void youtubeToMP3(String url) {//Download mp3 of youtube video using yt-dlp.exe. Ran from cmd - try { - ProcessBuilder builder = new ProcessBuilder( - "yt-dlp.exe", - "--extract-audio", - "--audio-format", "mp3", - "--audio-quality", "0", - "--output", "downloaded/%(title)s_%(id)s.mp3", - "--ffmpeg-location","ffmpeg.exe", - "--write-info-json", - url - ); - builder.redirectErrorStream(true); - Process p = builder.start(); - BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line; - while (true) { - line = r.readLine(); - if (line == null) { - break; - } - outputArea.setText(outputArea.getText()+"\n"+line); - System.out.println(line); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - - }
\ No newline at end of file diff --git a/src/main/java/TagEditorScreen.form b/src/main/java/TagEditorScreen.form index c82ca8e..828484c 100644 --- a/src/main/java/TagEditorScreen.form +++ b/src/main/java/TagEditorScreen.form @@ -73,7 +73,7 @@ <text value="Image"/> </properties> </component> - <component id="a2643" class="javax.swing.JTextField" binding="textField1" default-binding="true"> + <component id="a2643" class="javax.swing.JTextField" binding="imagePathField"> <constraints> <grid row="8" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> <preferred-size width="150" height="-1"/> diff --git a/src/main/java/TagEditorScreen.java b/src/main/java/TagEditorScreen.java index d964b28..764404c 100644 --- a/src/main/java/TagEditorScreen.java +++ b/src/main/java/TagEditorScreen.java @@ -9,8 +9,6 @@ import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; -import java.awt.image.CropImageFilter; -import java.awt.image.FilteredImageSource; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -21,7 +19,7 @@ public class TagEditorScreen extends JFrame{ private JLabel titleLabel; private JTextField uploaderField; private JLabel uploaderLabel; - private JTextField textField1; + private JTextField imagePathField; private JButton imageChooseButton; private JTable songTable; private JButton chooseAudioDirectoryButton; @@ -30,8 +28,10 @@ public class TagEditorScreen extends JFrame{ private JTextField textField2; private FileUtility fileUtil = new FileUtility(); private String setDirPath = ""; + private String selectedAlbumArt = ""; private ArrayList<File> songList = new ArrayList<File>(); private String currPath = ""; + private Boolean imageSelected = false; public TagEditorScreen(){ this.setContentPane(mainPanel); @@ -58,8 +58,7 @@ public class TagEditorScreen extends JFrame{ super.mouseClicked(e); populateFields(new File(songTable.getModel().getValueAt(songTable.getSelectedRow(),2 ).toString())); currPath = songTable.getModel().getValueAt(songTable.getSelectedRow() , 2).toString(); - - + imageSelected = false; } }); songTable.addKeyListener(new KeyAdapter() { @@ -70,6 +69,7 @@ public class TagEditorScreen extends JFrame{ try { populateFields(new File(songTable.getModel().getValueAt(songTable.getSelectedRow() + 1, 2).toString())); currPath = songTable.getModel().getValueAt(songTable.getSelectedRow() + 1, 2).toString(); + imageSelected = false; } catch(Exception ex){ @@ -79,6 +79,7 @@ public class TagEditorScreen extends JFrame{ try { populateFields(new File(songTable.getModel().getValueAt(songTable.getSelectedRow() - 1, 2).toString())); currPath = songTable.getModel().getValueAt(songTable.getSelectedRow() - 1, 2).toString(); + imageSelected = false; } catch(Exception ex){ @@ -95,6 +96,12 @@ public class TagEditorScreen extends JFrame{ Tag tag = f.getTag(); tag.setField(FieldKey.TITLE, titleField.getText()); tag.setField(FieldKey.ARTIST, uploaderField.getText()); + if(imageSelected){ + tag.deleteArtworkField(); + Artwork cover = Artwork.createArtworkFromFile(new File(selectedAlbumArt)); + tag.addField(cover); + System.out.println("Changed the Artwork"); + } f.commit(); clearSongTable(); songList = fileUtil.getMp3Files(setDirPath); //get arraylist of all files in the directory @@ -108,6 +115,26 @@ public class TagEditorScreen extends JFrame{ } }); + imageChooseButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + selectedAlbumArt = fileUtil.showImageFileChooser(); + File selectedFile = new File(selectedAlbumArt); + BufferedImage selectedImage = null; + selectedImage = ImageIO.read(selectedFile); + ImageIcon albumArtIcon = new ImageIcon(resizeImage(selectedImage, 260, 180)); + artIconLabel.setText(selectedFile.getName()); + artIconLabel.setIcon(albumArtIcon); + imageSelected = true; + + } + catch(Exception ex){ + + } + + } + }); } public void initializeTable(){ songTable.setDefaultEditor(Object.class, null); @@ -139,6 +166,7 @@ public class TagEditorScreen extends JFrame{ Artwork albumArt = tag.getFirstArtwork(); ImageIcon albumArtIcon = new ImageIcon(resizeImage(albumArt.getImage(),320,180)); artIconLabel.setIcon(albumArtIcon); + artIconLabel.setText(""); } |
