Skip to content

Commit

Permalink
Manual Tagging Improved
Browse files Browse the repository at this point in the history
- Added the ability to manually change album art
- Moved UI initialization code to bottom of file
  • Loading branch information
pinapelz committed Aug 22, 2022
1 parent db293e9 commit f1402e9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 78 deletions.
14 changes: 14 additions & 0 deletions src/main/java/FileUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ public static void deleteAllFilesDir(String path) {
}
}
}

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 {
Expand Down
142 changes: 70 additions & 72 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -33,6 +33,75 @@ public static void main(String[] args) {
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);
Expand Down Expand Up @@ -106,76 +175,5 @@ public void actionPerformed(ActionEvent e){
});
}

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();
}
}




}
2 changes: 1 addition & 1 deletion src/main/java/TagEditorScreen.form
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
38 changes: 33 additions & 5 deletions src/main/java/TagEditorScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -58,8 +58,7 @@ public void mouseClicked(MouseEvent e) {
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() {
Expand All @@ -70,6 +69,7 @@ public void keyPressed(KeyEvent e) {
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){

Expand All @@ -79,6 +79,7 @@ else if(e.getKeyCode()==KeyEvent.VK_UP){
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){

Expand All @@ -95,6 +96,12 @@ public void actionPerformed(ActionEvent e) {
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
Expand All @@ -108,6 +115,26 @@ public void actionPerformed(ActionEvent e) {

}
});
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);
Expand Down Expand Up @@ -139,6 +166,7 @@ public void populateFields(File audioFile){
Artwork albumArt = tag.getFirstArtwork();
ImageIcon albumArtIcon = new ImageIcon(resizeImage(albumArt.getImage(),320,180));
artIconLabel.setIcon(albumArtIcon);
artIconLabel.setText("");


}
Expand Down

0 comments on commit f1402e9

Please sign in to comment.