aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/Main.java
blob: 36ec8a994eaf1010c93d328f05f049dbb7168a4e (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.datatype.Artwork;

import javax.swing.*;
import javax.swing.text.DefaultCaret;


public class Main extends JFrame {
    String textPath = "";
    String formats[] = {"maxresdefault.jpg", "mqdefault.jpg", "hqdefault.jpg"};

    JPanel panel = new JPanel();
    JScrollPane scrollPane;
    JButton songsGen = new JButton("Generate text file");
    JButton editButton = new JButton("Edit Tags");
    JButton startButton = new JButton("Set .txt File");
    JCheckBox defaultFileBox = new JCheckBox("Use Default songs.txt file");
    JCheckBox useBlacklistBox = new JCheckBox("Use Blacklist.txt");
    JProgressBar progressBar = new JProgressBar();
    static JTextArea outputArea = new JTextArea("");
    JLabel title = new JLabel("YouTube to MP3 Auto Tagging [1]");

    int progress = 0;
    Boolean useBlacklist = false;
    Boolean readyState = false;
    Boolean useDefault = false;
    FileUtility fileUtil = new FileUtility();

    public Main() {
        initializeComponents();
        initializeActionsListeners();
        createDirectories();
    }

    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    private void downloadAndTag() {
        ArrayList<String> songs = fileUtil.txtToArrayList(textPath);
        progress = 0;
        String timeAppend = "";
        boolean partFlag = false;
        for (int i = 0; i < songs.size(); i++) {
            try {
                fileUtil.deleteAllFilesDir("downloaded");
                ArrayList<String> splitStamp = null;
                try {
                    splitStamp = new ArrayList<>(Arrays.asList(songs.get(i).split(",")));
                } catch (Exception e) {

                }
                if (splitStamp.size() >= 2) {
                    timeAppend = youtubeToMP3Part(splitStamp.get(0), splitStamp.get(1));
                    partFlag = true;
                } else {
                    youtubeToMP3Full(songs.get(i));
                }

                String info[] = fileUtil.parseJson(fileUtil.jsonToString(fileUtil.findJsonFile("downloaded"))); //title,uploader
                String uploader = info[1];
                String title = info[0];
                if (useBlacklist) {
                    System.out.println("Using blacklist");
                    uploader = fileUtil.removeBlacklist(uploader, "blacklist.txt");
                    title = fileUtil.removeBlacklist(title, "blacklist.txt");
                }
                AudioFile f = AudioFileIO.read(fileUtil.findMP3File("downloaded"));
                Tag tag = f.getTag();
                System.out.println("Uploader: " + uploader);
                System.out.println("Title: " + title);
                tag.setField(FieldKey.ARTIST, uploader);
                tag.setField(FieldKey.TITLE, title);
                fileUtil.downloadImage("https://img.youtube.com/vi/" + info[2] + "/", "img.jpg", formats);
                Artwork cover = Artwork.createArtworkFromFile(new File("img.jpg"));
                tag.addField(cover);
                f.commit();
                fileUtil.deleteFile("img.jpg");
                if (partFlag) {
                    fileUtil.moveFile(fileUtil.findMP3File("downloaded").getAbsolutePath(), "completed/"
                            + fileUtil.removeNonAlphaNumeric(info[0]) +
                            " [" + info[2] + "]" + timeAppend + ".mp3");
                } else {
                    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 youtubeToMP3Full(String url) {//Download mp3 of youtube video using yt-dlp.exe. Ran from cmd
        try {

            ProcessBuilder builder = new ProcessBuilder(
                    "yt-dlp.exe",
                    "-vU",
                    "--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();
            relayConsole(p);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "An Error occured while downloading using" +
                    " yt-dlp", "Error", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }

    }

    public static String youtubeToMP3Part(String url, String stamp) { //Download mp3 of youtube video using yt-dlp.exe. Ran from cmd
        System.out.println(url + " " + stamp);
        ArrayList<String> times = new ArrayList<>(Arrays.asList(stamp.split("-")));
        ArrayList<String> startTimeComponents = new ArrayList<>(Arrays.asList(times.get(0).split(":")));
        ArrayList<String> endTimeComponents = new ArrayList<>(Arrays.asList(times.get(1).split(":")));
        int startSec = 0;
        int endSec = 0;
        if (startTimeComponents.size() == 3) {
            startSec = Integer.parseInt(startTimeComponents.get(0)) * 60 * 60 + Integer.parseInt(startTimeComponents.get(1)) *
                    60 + Integer.parseInt(startTimeComponents.get(2));
        } else if (startTimeComponents.size() == 2) {
            startSec = Integer.parseInt(startTimeComponents.get(0)) * 60 + Integer.parseInt(startTimeComponents.get(1));
        }
        if (endTimeComponents.size() == 3) {
            endSec = Integer.parseInt(endTimeComponents.get(0)) * 60 * 60 + Integer.parseInt(endTimeComponents.get(1))
                    * 60 + Integer.parseInt(endTimeComponents.get(2));
        } else if (endTimeComponents.size() == 2) {
            endSec = Integer.parseInt(endTimeComponents.get(0)) * 60 + Integer.parseInt(endTimeComponents.get(1));
        }
        try {
            ProcessBuilder builder = new ProcessBuilder(
                    "yt-dlp.exe",
                    "-vU",
                    "--extract-audio",
                    "--audio-format", "mp3",
                    "--audio-quality", "0",
                    "--output", "downloaded/%(title)s_%(id)s.mp3",
                    "--ffmpeg-location", "ffmpeg.exe",
                    "--write-info-json", "--download-sections", "\"*" + startSec + "-" + endSec + "\"",
                    "--force-keyframes-at-cuts",
                    url
            );
            builder.redirectErrorStream(true);
            Process p = builder.start();
            relayConsole(p);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "An Error occured while downloading using" +
                    " yt-dlp", "Error", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
        return startSec + "to" + endSec;
    }


    private void initializeComponents() {//Initiate GUI components
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.add(panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        scrollPane = new JScrollPane(outputArea);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        outputArea.setEditable(true);
        outputArea.setLineWrap(true);
        DefaultCaret caret = (DefaultCaret) outputArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        panel.add(Box.createRigidArea(new Dimension(0, 5)));
        panel.setBorder(BorderFactory.createEmptyBorder(25, 10, 20, 10));
        startButton.setAlignmentX(CENTER_ALIGNMENT);
        title.setAlignmentX(Component.CENTER_ALIGNMENT);
        defaultFileBox.setAlignmentX(Component.CENTER_ALIGNMENT);
        useBlacklistBox.setAlignmentX(Component.CENTER_ALIGNMENT);
        editButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        songsGen.setAlignmentX(Component.CENTER_ALIGNMENT);
        progressBar.setStringPainted(true);
        title.setFont(new Font("Verdana", Font.PLAIN, 14));
        panel.add(title);
        panel.add(Box.createVerticalStrut(10));
        panel.add(progressBar);
        panel.add(Box.createVerticalStrut(10));
        panel.add(startButton);
        panel.add(defaultFileBox);
        panel.add(Box.createVerticalStrut(8));
        panel.add(scrollPane);
        panel.add(Box.createVerticalStrut(5));
        panel.add(editButton);
        panel.add(useBlacklistBox);
        panel.add(Box.createVerticalStrut(8));
        this.setSize(550, 450);
        this.setTitle("YTMP3Tagger");

    }

    private void initializeActionsListeners() {//Add all actionlisteners for buttons
        defaultFileBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File f = new File("songs.txt");
                if (f.exists() & !f.isDirectory() && !useDefault) {
                    System.out.println("songs found");
                    textPath = "songs.txt";
                    showWarning("Default File has been set.\nMake sure you add a new line for each URL");
                    readyState = true;
                    startButton.setText("Start Download");
                    outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button");
                    System.out.println("Ready to begin downloading. Press the button");
                    useDefault = true;

                } else {
                    useDefault = false;
                    readyState = false;
                    startButton.setText("Set .txt file");
                }
            }
        });
        useBlacklistBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (useBlacklistBox.isSelected()) {
                    useBlacklist = true;
                } else {
                    useBlacklist = false;
                }

            }
        });

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (readyState == false) {
                    outputArea.setText(outputArea.getText() + "\n" + "txt path has not been set. Launching chooserPane");
                    System.out.println(".txt path has not been set. Launching chooserPane");
                    textPath = fileUtil.showTextFileChooser();
                    try {
                        if (!textPath.equals("")) {
                            showWarning("File has been set.\nMake sure you add a new line for each URL");
                            readyState = true;
                            startButton.setText("Start Download");
                            outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button");
                            System.out.println("Ready to begin downloading. Press the button");
                        }
                    } catch (Exception ex) {

                    }
                } else {
                    Runnable runnable = () -> {
                        outputArea.setText("");
                        startButton.setEnabled(false);
                        downloadAndTag();
                        startButton.setEnabled(true);

                    };
                    Thread thread = new Thread(runnable);
                    thread.start();
                }
            }
        });
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new TagEditorScreen().setVisible(true);
            }
        });
        songsGen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
    }

    public static void relayConsole(Process p) {
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String cmd_line;
        while (true) {
            try {
                cmd_line = r.readLine();
                if (cmd_line == null) {
                    break;
                }
                outputArea.setText(outputArea.getText() + "\n" + cmd_line);
                System.out.println(cmd_line);
            }
            catch (IOException e) {
                System.out.println("Error while relaying from CMD");
            }
        }
    }

    public void createDirectories(){
        File f = new File("downloaded");
        if (!f.exists()) {
            f.mkdir();
        }
        File f2 = new File("completed");
        if (!f2.exists()) {
            f2.mkdir();
        }
    }

    public static void showWarning(String message) {
        JOptionPane.showMessageDialog(null, message, "JUST YOUR FRIENDLY NEIGHBORLY REMINDER", JOptionPane.WARNING_MESSAGE);
    }


}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage