diff options
| author | Donald Shan <xxpinapelzxx@gmail.com> | 2022-08-18 17:10:06 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-18 17:10:06 -0700 |
| commit | 2deeb3748c83c10d01500e189a8fed8352b839e8 (patch) | |
| tree | fcc5695175666bab25931af326ea46c732b32f7c | |
| parent | a571cd01967b8b6c3f63955b68bd88c6f89f8975 (diff) | |
Add files via upload
| -rw-r--r-- | pom.xml | 97 | ||||
| -rw-r--r-- | src/main/java/org/example/Main.java | 176 | ||||
| -rw-r--r-- | src/test/java/org/example/AppTest.java | 20 | ||||
| -rw-r--r-- | yt-dlp.exe | bin | 0 -> 13733484 bytes |
4 files changed, 293 insertions, 0 deletions
@@ -0,0 +1,97 @@ +<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.example</groupId>
+ <artifactId>YouTubeMp3AutoTag</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <name>YouTubeMp3AutoTag</name>
+ <!-- FIXME change it to the project's website -->
+ <url>http://www.example.com</url>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <maven.compiler.source>1.7</maven.compiler.source>
+ <maven.compiler.target>1.7</maven.compiler.target>
+ </properties>
+ <repositories>
+ <repository>
+ <id>jitpack.io</id>
+ <url>https://jitpack.io</url>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.11</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.mpatric</groupId>
+ <artifactId>mp3agic</artifactId>
+ <version>0.9.1</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.8.0</version>
+ </dependency>
+ <!-- https://mvnrepository.com/artifact/org/jaudiotagger -->
+ <dependency>
+ <groupId>org</groupId>
+ <artifactId>jaudiotagger</artifactId>
+ <version>2.0.3</version>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
+ <plugins>
+ <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
+ <plugin>
+ <artifactId>maven-clean-plugin</artifactId>
+ <version>3.1.0</version>
+ </plugin>
+ <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
+ <plugin>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>3.0.2</version>
+ </plugin>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.8.0</version>
+ </plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.22.1</version>
+ </plugin>
+ <plugin>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>3.0.2</version>
+ </plugin>
+ <plugin>
+ <artifactId>maven-install-plugin</artifactId>
+ <version>2.5.2</version>
+ </plugin>
+ <plugin>
+ <artifactId>maven-deploy-plugin</artifactId>
+ <version>2.8.2</version>
+ </plugin>
+ <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
+ <plugin>
+ <artifactId>maven-site-plugin</artifactId>
+ <version>3.7.1</version>
+ </plugin>
+ <plugin>
+ <artifactId>maven-project-info-reports-plugin</artifactId>
+ <version>3.0.0</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+</project>
diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..ee8bd2a --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,176 @@ +package org.example;
+import java.io.*;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.regex.Matcher;
+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 java.util.regex.Pattern;
+
+public class Main {
+
+ public static void main(String[] args) {
+ ArrayList<String> songs = readFile("songs.txt");
+ for(int i = 0;i<songs.size();i++) {
+ try {
+ deleteFiles("downloaded");
+ downloadYouTube(songs.get(i));
+ String info[] = parseJson(readJson(findJsonFile("downloaded"))); //title,uploader
+ String uploader = info[1];
+ String title = info[0];
+ AudioFile f = AudioFileIO.read(findMP3File("downloaded"));
+ Tag tag = f.getTag();
+ tag.setField(FieldKey.ARTIST, uploader);
+ tag.setField(FieldKey.TITLE, title);
+ 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();
+ moveFile(findMP3File("downloaded").getAbsolutePath(), "completed/" + info[0] + ".mp3");
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ }
+ }
+
+ }
+ //download image using url
+ public static void downloadImage(String url, String fileName) throws IOException {
+ FileOutputStream fos = new FileOutputStream(fileName);
+ URL urlObj = new URL(url);
+ InputStream is = urlObj.openStream();
+ byte[] b = new byte[2048];
+ int length;
+ while ((length = is.read(b)) != -1) {
+ fos.write(b, 0, length);
+ }
+ fos.close();
+ is.close();
+ }
+
+ public static void moveFile(String source, String destination) {
+ File sourceFile = new File(source);
+ File destinationFile = new File(destination);
+ sourceFile.renameTo(destinationFile);
+ System.out.println("Moving file to Completed Folder");
+ }
+
+ public static File findMP3File(String directory){
+ File dir = new File(directory);
+ File[] files = dir.listFiles();
+ for(File file : files){
+ if(file.getName().endsWith(".mp3")){
+ return file;
+ }
+ }
+ return null;
+ }
+
+ public static String findJsonFile(String folderName) {
+ File folder = new File(folderName);
+ File[] listOfFiles = folder.listFiles();
+ for (int i = 0; i < listOfFiles.length; i++) {
+ if (listOfFiles[i].isFile()) {
+ if (listOfFiles[i].getName().endsWith(".json")) {
+ return listOfFiles[i].getAbsolutePath();
+ }
+ }
+ }
+ return null;
+ }
+ public static String readJson(String fileName) {
+ String json = "";
+ try {
+ BufferedReader br = new BufferedReader(new FileReader(fileName));
+ StringBuilder sb = new StringBuilder();
+ String line = br.readLine();
+ while (line != null) {
+ sb.append(line);
+ line = br.readLine();
+ }
+ json = sb.toString();
+ br.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return json;
+ }
+
+ public static String[] parseJson(String json) {
+ String title = "";
+ String uploader = "";
+ String id = "";
+ Pattern titlePattern = Pattern.compile("\"fulltitle\": \"(.*?)\",");
+ Matcher titleMatcher = titlePattern.matcher(json);
+ Pattern uploaderPattern = Pattern.compile("\"uploader\": \"(.*?)\",");
+ Matcher uploaderMatcher = uploaderPattern.matcher(json);
+ Pattern idPattern = Pattern.compile("\"id\": \"(.*?)\",");
+ Matcher idMatcher = idPattern.matcher(json);
+ titleMatcher.find();
+ idMatcher.find();
+ uploaderMatcher.find();
+ title = titleMatcher.group(1);
+ uploader = uploaderMatcher.group(1);
+ id = idMatcher.group(1);
+ String[] info = {title,uploader,id};
+ return info;
+
+ }
+ public static void downloadYouTube(String url) {
+ 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;
+ }
+ System.out.println(line);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ public static void deleteFiles(String path) {
+ File folder = new File(path);
+ File[] files = folder.listFiles();
+ if (files != null) {
+ for (File f : files) {
+ f.delete();
+ }
+ }
+ }
+ public static ArrayList<String> readFile(String fileName) {
+ ArrayList<String> lines = new ArrayList<String>();
+ try {
+ FileReader fr = new FileReader(fileName);
+ BufferedReader br = new BufferedReader(fr);
+ String line;
+ while ((line = br.readLine()) != null) {
+ lines.add(line);
+ }
+ br.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return lines;
+ }
+
+
+}
diff --git a/src/test/java/org/example/AppTest.java b/src/test/java/org/example/AppTest.java new file mode 100644 index 0000000..9a3da2b --- /dev/null +++ b/src/test/java/org/example/AppTest.java @@ -0,0 +1,20 @@ +package org.example;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest
+{
+ /**
+ * Rigorous Test :-)
+ */
+ @Test
+ public void shouldAnswerWithTrue()
+ {
+ assertTrue( true );
+ }
+}
diff --git a/yt-dlp.exe b/yt-dlp.exe Binary files differnew file mode 100644 index 0000000..c559791 --- /dev/null +++ b/yt-dlp.exe |
