Reworked how mp3s are being read and loaded int player
This commit is contained in:
@@ -47,6 +47,7 @@ import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.locusworks.discord.eighttrack.audio.Mp3UploadHandler;
|
||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
||||
import net.locusworks.discord.eighttrack.services.GuildMusicService;
|
||||
@@ -147,7 +148,7 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
String command = commands.remove(0);
|
||||
|
||||
switch(command) {
|
||||
case "-upload":
|
||||
case "+upload":
|
||||
gmh.upload(event);
|
||||
return;
|
||||
case "-play":
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Project: Eight Track, File: EightTrackAudioSendHandler.java
|
||||
*
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
* modified, duplicated, adapted or translated into another program language in any
|
||||
* form or by any means, electronic, mechanical, photocopying, recording, or
|
||||
* otherwise, without the prior written permission from Locusworks. Locusworks
|
||||
* affirms that Eight-Track(R) software and data is subject to United States
|
||||
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
|
||||
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
|
||||
*
|
||||
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
||||
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
|
||||
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
|
||||
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
|
||||
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
|
||||
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
|
||||
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
|
||||
* ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*/
|
||||
package net.locusworks.discord.eighttrack.handlers;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
|
||||
|
||||
import net.dv8tion.jda.api.audio.AudioSendHandler;
|
||||
|
||||
public class EightTrackAudioSendHandler implements AudioSendHandler {
|
||||
|
||||
private final AudioPlayer audioPlayer;
|
||||
private AudioFrame lastFrame;
|
||||
|
||||
public EightTrackAudioSendHandler(AudioPlayer audioPlayer) {
|
||||
this.audioPlayer = audioPlayer;
|
||||
}
|
||||
|
||||
public boolean canProvide() {
|
||||
lastFrame = audioPlayer.provide();
|
||||
return lastFrame != null;
|
||||
}
|
||||
|
||||
public ByteBuffer provide20MsAudio() {
|
||||
if (lastFrame == null || lastFrame.getData() == null) {
|
||||
return ByteBuffer.wrap(new byte[0]);
|
||||
}
|
||||
return ByteBuffer.wrap(lastFrame.getData());
|
||||
}
|
||||
|
||||
public boolean isOpus() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,7 +29,6 @@ package net.locusworks.discord.eighttrack.handlers;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.OffsetDateTime;
|
||||
@@ -46,14 +45,12 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
|
||||
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.ChannelType;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.GuildChannel;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.Message.Attachment;
|
||||
@@ -64,13 +61,16 @@ import net.dv8tion.jda.api.exceptions.ErrorResponseException;
|
||||
import net.dv8tion.jda.api.managers.AudioManager;
|
||||
import net.dv8tion.jda.api.requests.ErrorResponse;
|
||||
import net.locusworks.crypto.utils.HashUtils;
|
||||
import net.locusworks.discord.eighttrack.audio.EightTrackAudioSendHandler;
|
||||
import net.locusworks.discord.eighttrack.audio.Mp3UploadHandler;
|
||||
import net.locusworks.discord.eighttrack.audio.Mp3UploadResults;
|
||||
import net.locusworks.discord.eighttrack.audio.TrackManager;
|
||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
|
||||
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||
import net.locusworks.discord.eighttrack.database.entities.Song;
|
||||
import net.locusworks.discord.eighttrack.listeners.ReactionListener;
|
||||
import net.locusworks.discord.eighttrack.scheduler.TrackScheduler;
|
||||
import net.locusworks.discord.eighttrack.services.GuildSongRepoService;
|
||||
import net.locusworks.discord.eighttrack.utils.Reactions;
|
||||
import net.locusworks.logger.ApplicationLogger;
|
||||
@@ -79,10 +79,7 @@ import net.locusworks.logger.ApplicationLoggerFactory;
|
||||
public class GuildMusicHandler {
|
||||
|
||||
private Path musicDir;
|
||||
private TrackScheduler ts;
|
||||
private DefaultAudioPlayerManager apm;
|
||||
private ApplicationLogger logger;
|
||||
private AudioPlayer player;
|
||||
private AtomicBoolean playing;
|
||||
private Long voiceChannelId;
|
||||
|
||||
@@ -95,6 +92,8 @@ public class GuildMusicHandler {
|
||||
private long guildId;
|
||||
|
||||
private GuildPlaylist currentPlaylist;
|
||||
|
||||
private TrackManager trackManager;
|
||||
|
||||
public GuildMusicHandler(Path musicDir, long guildId, Mp3UploadHandler uploadHandler, ReactionHandler reactionHandler, GuildSongRepoService guildSongRepoService) throws IOException {
|
||||
this.logger = ApplicationLoggerFactory.getLogger(GuildMusicHandler.class);
|
||||
@@ -105,13 +104,7 @@ public class GuildMusicHandler {
|
||||
this.guildSongRepoService = guildSongRepoService;
|
||||
this.guildId = guildId;
|
||||
this.reactionHandler = reactionHandler;
|
||||
this.apm = new DefaultAudioPlayerManager();
|
||||
|
||||
AudioSourceManagers.registerLocalSource(apm);
|
||||
this.player = apm.createPlayer();
|
||||
|
||||
this.ts = new TrackScheduler();
|
||||
player.addListener(ts);
|
||||
this.trackManager = new TrackManager();
|
||||
}
|
||||
|
||||
public void playlist(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
|
||||
@@ -168,10 +161,10 @@ public class GuildMusicHandler {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (ts.peek() == null) {
|
||||
if (!trackManager.hasTracks()) {
|
||||
loadRandomSong();
|
||||
}
|
||||
AudioTrack track = ts.peek();
|
||||
AudioTrack track = trackManager.play();
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
.setAuthor(event.getMember().getEffectiveName(), null, event.getAuthor().getAvatarUrl())
|
||||
.setTitle("Next Up:")
|
||||
@@ -183,7 +176,7 @@ public class GuildMusicHandler {
|
||||
|
||||
public void repeat(GuildMessageReceivedEvent event) throws Exception {
|
||||
next(event);
|
||||
ts.setFinishedCallback((ater) -> {
|
||||
trackManager.setFinishedCallback((ater) -> {
|
||||
if (!playing.get()) return;
|
||||
try {
|
||||
next(event);
|
||||
@@ -222,7 +215,7 @@ public class GuildMusicHandler {
|
||||
}
|
||||
|
||||
public void stop(GuildMessageReceivedEvent event, boolean stoppedFromRepeat) {
|
||||
player.stopTrack();
|
||||
trackManager.stopTrack();
|
||||
voiceChannelId = null;
|
||||
if (!stoppedFromRepeat) {
|
||||
currentPlaylist = null;
|
||||
@@ -261,7 +254,7 @@ public class GuildMusicHandler {
|
||||
AudioManager manager = event.getGuild().getAudioManager();
|
||||
manager.openAudioConnection(vc);
|
||||
|
||||
manager.setSendingHandler(new EightTrackAudioSendHandler(player));
|
||||
manager.setSendingHandler(new EightTrackAudioSendHandler(trackManager));
|
||||
|
||||
if (commands != null && !commands.isEmpty()) {
|
||||
String uuid = commands.remove(0);
|
||||
@@ -273,14 +266,18 @@ public class GuildMusicHandler {
|
||||
stop(event, true);
|
||||
}
|
||||
|
||||
if (!ts.hasTracks()) {
|
||||
if (!trackManager.hasTracks()) {
|
||||
loadRandomSong();
|
||||
}
|
||||
|
||||
if (ts.hasTracks()) {
|
||||
if (trackManager.hasTracks()) {
|
||||
lastPlayed = OffsetDateTime.now();
|
||||
|
||||
AudioTrack track = ts.getNextTrack();
|
||||
AudioTrack track = trackManager.play();
|
||||
if (track == null) {
|
||||
logger.warn("Unable to find track: " + trackManager.getCurrentItem()) ;
|
||||
return;
|
||||
}
|
||||
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
.setAuthor(event.getMember().getEffectiveName(), null, event.getAuthor().getAvatarUrl())
|
||||
@@ -290,7 +287,7 @@ public class GuildMusicHandler {
|
||||
.build();
|
||||
|
||||
event.getChannel().sendMessage(embed).queue();
|
||||
player.playTrack(track);
|
||||
trackManager.playTrack(track);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -358,10 +355,19 @@ public class GuildMusicHandler {
|
||||
sb.append("```");
|
||||
event.getChannel().sendMessage(sb.toString()).queue();
|
||||
}
|
||||
|
||||
|
||||
public void adminPlaylist(GuildMessageReceivedEvent event, List<String> commands) {
|
||||
if(!isAdmin(event)) return;
|
||||
if (!checkCommands(event, commands, event.getMember().getAsMention() + ", valid commands are +playst list, play, delete")) return;
|
||||
|
||||
String command = commands.remove(0);
|
||||
switch (command) {
|
||||
case "list":
|
||||
listAllPlaylists(event);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void delete(GuildMessageReceivedEvent event, List<String> commands) {
|
||||
@@ -393,11 +399,11 @@ public class GuildMusicHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
ts.clearTracks();
|
||||
trackManager.clearTracks();
|
||||
|
||||
for(GuildPlaylistSong gpls : guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylist(currentPlaylist)) {
|
||||
Song s = gpls.getGuildSong().getSong();
|
||||
apm.loadItem(s.getFilePath(), ts).get();
|
||||
trackManager.queueLast(s.getFilePath());
|
||||
}
|
||||
|
||||
playing.set(false);
|
||||
@@ -433,7 +439,7 @@ public class GuildMusicHandler {
|
||||
}
|
||||
|
||||
String hash = HashUtils.hash("SHA-1", result.getData());
|
||||
|
||||
|
||||
GuildSong gs = guildSongRepoService.getGuildSongRepo().findByGuildAndSongHash(event.getGuild().getIdLong(), hash);
|
||||
if (gs != null) {
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
@@ -446,7 +452,7 @@ public class GuildMusicHandler {
|
||||
.build();
|
||||
return embed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Song song = guildSongRepoService.getSongRepo().findByFileHash(hash);
|
||||
if (song == null) {
|
||||
@@ -471,8 +477,6 @@ public class GuildMusicHandler {
|
||||
song.setArtist(result.getArtist());
|
||||
song.setDateAdded(new Date());
|
||||
song.setDiscNumber(result.getDiscNumber());
|
||||
if (result.getDuration() != null)
|
||||
song.setDuration(BigInteger.valueOf(result.getDuration()));
|
||||
song.setFileHash(hash);
|
||||
song.setFilePath(output.toAbsolutePath().toString());
|
||||
song.setGenre(result.getGenre());
|
||||
@@ -494,21 +498,23 @@ public class GuildMusicHandler {
|
||||
|
||||
guildSongRepoService.getGuildSongRepo().save(gs);
|
||||
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
EmbedBuilder builder = new EmbedBuilder()
|
||||
.setColor(Color.GREEN)
|
||||
.setThumbnail(event.getGuild().getIconUrl())
|
||||
.setTitle("Upload Results for " + fileName)
|
||||
.addField("Title", result.getTitle(), true)
|
||||
.addField("Artist", result.getArtist(), true)
|
||||
.addField("Album", result.getAlbum(), true)
|
||||
.addField("Track", result.getTrackNumber(), true)
|
||||
.addField("Disc", result.getDiscNumber(), true)
|
||||
.addField("Year", result.getReleaseDate(), true)
|
||||
.addField("Duration", result.getDurationFormat(), true)
|
||||
.addField("ID", gs.getUuid(), true)
|
||||
.setTimestamp(OffsetDateTime.now())
|
||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||
.build();
|
||||
.addField("Title", result.getTitle(), true);
|
||||
|
||||
if (!StringUtils.isBlank(result.getArtist())) builder.addField("Artist", result.getArtist(), true);
|
||||
if (!StringUtils.isBlank(result.getAlbum())) builder.addField("Album", result.getAlbum(), true);
|
||||
if (!StringUtils.isBlank(result.getTrackNumber())) builder.addField("Track", result.getTrackNumber(), true);
|
||||
if (!StringUtils.isBlank(result.getDiscNumber())) builder.addField("Disc", result.getDiscNumber(), true);
|
||||
if (!StringUtils.isBlank(result.getReleaseDate())) builder.addField("Year", result.getReleaseDate(), true);
|
||||
|
||||
builder.addField("ID", gs.getUuid(), true);
|
||||
|
||||
MessageEmbed embed = builder.build();
|
||||
|
||||
return embed;
|
||||
}
|
||||
@@ -615,10 +621,16 @@ public class GuildMusicHandler {
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void listAllPlaylists(GuildMessageReceivedEvent event) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
private void deleteUserPlayList(GuildMessageReceivedEvent event, List<String> commands) {
|
||||
// TODO Auto-generated method stub
|
||||
System.out.println(commands.stream().collect(Collectors.joining(", ")));
|
||||
Guild guild = event.getGuild();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void deleteGuildSongsConfirm(List<GuildSong> songs, String user, Message msg) {
|
||||
@@ -823,19 +835,17 @@ public class GuildMusicHandler {
|
||||
int item = random.nextInt(gsList.size());
|
||||
|
||||
GuildSong song = gsList.get(item);
|
||||
|
||||
apm.loadItem(song.getSong().getFilePath(), ts).get();
|
||||
trackManager.queueNext(song.getSong().getFilePath());
|
||||
}
|
||||
|
||||
private boolean findSong(String uuid) throws Exception {
|
||||
GuildSong gs = guildSongRepoService.getGuildSongRepo().findByUuid(uuid);
|
||||
if (gs == null) return false;
|
||||
|
||||
apm.loadItem(gs.getSong().getFilePath(), ts).get();
|
||||
|
||||
trackManager.queueNext(gs.getSong().getFilePath());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private boolean isAdmin(GuildMessageReceivedEvent event) {
|
||||
if (!event.getMessage().getMember().hasPermission(Permission.ADMINISTRATOR, Permission.MANAGE_SERVER)) {
|
||||
event.getChannel().sendMessage(String.format("Sorry <@%s> i can't do that. *psst: you have to have manage server, or administrator role*", event.getMember().getIdLong())).queue();
|
||||
@@ -843,7 +853,7 @@ public class GuildMusicHandler {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private boolean checkCommands(GuildMessageReceivedEvent event, List<String> commands, String msg) {
|
||||
if (commands == null || commands.isEmpty()) {
|
||||
event.getChannel().sendMessage(msg).queue();
|
||||
@@ -860,5 +870,5 @@ public class GuildMusicHandler {
|
||||
if (ex.getErrorResponse() != ErrorResponse.UNKNOWN_MESSAGE) throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Project: Eight Track, File: Mp3UploadHandler.java
|
||||
*
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
* modified, duplicated, adapted or translated into another program language in any
|
||||
* form or by any means, electronic, mechanical, photocopying, recording, or
|
||||
* otherwise, without the prior written permission from Locusworks. Locusworks
|
||||
* affirms that Eight-Track(R) software and data is subject to United States
|
||||
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
|
||||
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
|
||||
*
|
||||
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
||||
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
|
||||
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
|
||||
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
|
||||
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
|
||||
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
|
||||
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
|
||||
* ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*/
|
||||
package net.locusworks.discord.eighttrack.handlers;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.apache.tika.exception.TikaException;
|
||||
import org.apache.tika.metadata.Metadata;
|
||||
import org.apache.tika.parser.ParseContext;
|
||||
import org.apache.tika.parser.mp3.Mp3Parser;
|
||||
import org.apache.tika.sax.BodyContentHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
@Component
|
||||
public class Mp3UploadHandler {
|
||||
|
||||
public Mp3UploadResults parse(Path file) throws IOException {
|
||||
InputStream is = Files.newInputStream(file);
|
||||
return parse(is);
|
||||
}
|
||||
|
||||
public Mp3UploadResults parse(InputStream is) throws IOException {
|
||||
byte[] data;
|
||||
try(InputStream in = is; ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
int read = 0;
|
||||
byte[] buffer = new byte[1024 * 1024];
|
||||
while((read = in.read(buffer)) > 0) {
|
||||
baos.write(buffer, 0, read);
|
||||
}
|
||||
data = baos.toByteArray();
|
||||
}
|
||||
|
||||
try (InputStream in = new ByteArrayInputStream(data)) {
|
||||
BodyContentHandler handler = new BodyContentHandler();
|
||||
Metadata metadata = new Metadata();
|
||||
ParseContext context = new ParseContext();
|
||||
|
||||
Mp3Parser parser = new Mp3Parser();
|
||||
|
||||
parser.parse(in, handler, metadata, context);
|
||||
|
||||
return new Mp3UploadResults(metadata, data);
|
||||
} catch (Exception ex) {
|
||||
throw new IOException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws IOException, SAXException, TikaException {
|
||||
String file = "E:\\Music2\\Alan Walker\\Itinerary_ Dallas.pdf";
|
||||
Path path = Paths.get(file);
|
||||
|
||||
Mp3UploadHandler fuh = new Mp3UploadHandler();
|
||||
fuh.parse(path);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,216 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Project: Eight Track, File: Mp3UploadResults.java
|
||||
*
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
* modified, duplicated, adapted or translated into another program language in any
|
||||
* form or by any means, electronic, mechanical, photocopying, recording, or
|
||||
* otherwise, without the prior written permission from Locusworks. Locusworks
|
||||
* affirms that Eight-Track(R) software and data is subject to United States
|
||||
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
|
||||
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
|
||||
*
|
||||
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
||||
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
|
||||
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
|
||||
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
|
||||
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
|
||||
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
|
||||
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
|
||||
* ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*/
|
||||
package net.locusworks.discord.eighttrack.handlers;
|
||||
|
||||
import org.apache.tika.metadata.Metadata;
|
||||
|
||||
public class Mp3UploadResults {
|
||||
|
||||
private enum MetaDataField {
|
||||
GENRE("xmpDM:genre"),
|
||||
COMPOSER("xmpDM:composer"),
|
||||
ALBUM("xmpDM:album"),
|
||||
TRACK_NUMBER("xmpDM:trackNumber"),
|
||||
DISC_NUMBER("xmpDM:discNumber"),
|
||||
ARTIST("xmpDM:artist"),
|
||||
TITLE("title"),
|
||||
RELEASE_DATE("xmpDM:releaseDate"),
|
||||
DURATION("xmpDM:duration");
|
||||
|
||||
private String value;
|
||||
|
||||
private MetaDataField(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
||||
private String genre;
|
||||
private String composure;
|
||||
private String album;
|
||||
private String trackNumber;
|
||||
private String discNumber;
|
||||
private String artist;
|
||||
private String title;
|
||||
private String releaseDate;
|
||||
private Long duration;
|
||||
|
||||
private byte[] rawData;
|
||||
|
||||
public Mp3UploadResults( Metadata metadata, byte[] data) {
|
||||
parseResults(metadata);
|
||||
this.rawData = data;
|
||||
}
|
||||
|
||||
private void parseResults(Metadata metadata) {
|
||||
genre = metadata.get(MetaDataField.GENRE.getValue());
|
||||
composure = metadata.get(MetaDataField.COMPOSER.getValue());
|
||||
album = metadata.get(MetaDataField.ALBUM.getValue());
|
||||
trackNumber = metadata.get(MetaDataField.TRACK_NUMBER.getValue());
|
||||
discNumber = metadata.get(MetaDataField.DISC_NUMBER.getValue());
|
||||
artist = metadata.get(MetaDataField.ARTIST.getValue());
|
||||
title = metadata.get(MetaDataField.TITLE.getValue());
|
||||
releaseDate = metadata.get(MetaDataField.RELEASE_DATE.getValue());
|
||||
|
||||
String durationStr = metadata.get(MetaDataField.DURATION.getValue()).trim();
|
||||
if (durationStr != null) {
|
||||
duration = Double.valueOf(metadata.get(MetaDataField.DURATION.getValue())).longValue();
|
||||
}
|
||||
}
|
||||
|
||||
public final byte[] getData() {
|
||||
return this.rawData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the genre
|
||||
*/
|
||||
public final String getGenre() {
|
||||
return genre;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the composure
|
||||
*/
|
||||
public final String getComposure() {
|
||||
return composure;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the album
|
||||
*/
|
||||
public final String getAlbum() {
|
||||
return album;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the trackNumber
|
||||
*/
|
||||
public final String getTrackNumber() {
|
||||
return trackNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the discNumber
|
||||
*/
|
||||
public final String getDiscNumber() {
|
||||
return discNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the artist
|
||||
*/
|
||||
public final String getArtist() {
|
||||
return artist;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the title
|
||||
*/
|
||||
public final String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the releaseDate
|
||||
*/
|
||||
public final String getReleaseDate() {
|
||||
return releaseDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the duration
|
||||
*/
|
||||
public final Long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public final String getDurationFormat() {
|
||||
return dateFormat(duration);
|
||||
}
|
||||
|
||||
public boolean validFile() {
|
||||
return getTitle() != null && !getTitle().isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (getTitle() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Title", getTitle()));
|
||||
|
||||
if (getArtist() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Artist", getArtist()));
|
||||
|
||||
if (getAlbum() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Album", getAlbum()));
|
||||
|
||||
if (getTrackNumber() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Track", getTrackNumber()));
|
||||
|
||||
if (getDiscNumber() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Disc", getDiscNumber()));
|
||||
|
||||
if (getReleaseDate() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Year", getReleaseDate()));
|
||||
|
||||
if (getGenre() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Genre", getGenre()));
|
||||
|
||||
if (duration != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Duration", dateFormat(getDuration())));
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
rawData = new byte[0];
|
||||
}
|
||||
|
||||
private String dateFormat(long timeInMilliSeconds) {
|
||||
long seconds = timeInMilliSeconds / 1000;
|
||||
long minutes = seconds / 60;
|
||||
long hours = minutes / 60;
|
||||
long days = hours / 24;
|
||||
|
||||
long sec = seconds % 60;
|
||||
long min = minutes % 60;
|
||||
long hr = hours % 24;
|
||||
|
||||
String time = "" + sec;
|
||||
if (min > 0) time = min + ":" + time;
|
||||
if (hr > 0) time = hr + ":" + time;
|
||||
if (days > 0) time = days + ":" + time;
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user