use of org.jaudiotagger.tag.id3.ID3v1Tag in project MusicDNA by harjot-oberai.
the class Lyrics3v2 method write.
/**
* @param file
* @throws IOException
*/
public void write(RandomAccessFile file) throws IOException {
int offset = 0;
long size;
long filePointer;
byte[] buffer = new byte[6 + 9];
String str;
Lyrics3v2Field field;
Iterator<Lyrics3v2Field> iterator;
ID3v1Tag id3v1tag;
new ID3v1Tag();
id3v1tag = null;
delete(file);
file.seek(file.length());
filePointer = file.getFilePointer();
str = "LYRICSBEGIN";
for (int i = 0; i < str.length(); i++) {
buffer[i] = (byte) str.charAt(i);
}
file.write(buffer, 0, str.length());
// IND needs to go first. lets createField/update it and write it first.
updateField("IND");
field = fieldMap.get("IND");
field.write(file);
iterator = fieldMap.values().iterator();
while (iterator.hasNext()) {
field = iterator.next();
String id = field.getIdentifier();
boolean save = TagOptionSingleton.getInstance().getLyrics3SaveField(id);
if ((!id.equals("IND")) && save) {
field.write(file);
}
}
size = file.getFilePointer() - filePointer;
if (this.getSize() != size) {
//logger.config("Lyrics3v2 size didn'timer match up while writing.");
//logger.config("this.getsize() = " + this.getSize());
//logger.config("size (filePointer) = " + size);
}
str = Long.toString(size);
for (int i = 0; i < (6 - str.length()); i++) {
buffer[i] = (byte) '0';
}
offset += (6 - str.length());
for (int i = 0; i < str.length(); i++) {
buffer[i + offset] = (byte) str.charAt(i);
}
offset += str.length();
str = "LYRICS200";
for (int i = 0; i < str.length(); i++) {
buffer[i + offset] = (byte) str.charAt(i);
}
offset += str.length();
file.write(buffer, 0, offset);
if (id3v1tag != null) {
id3v1tag.write(file);
}
}
use of org.jaudiotagger.tag.id3.ID3v1Tag in project frostwire by frostwire.
the class SoundcloudDownload method setAlbumArt.
private static boolean setAlbumArt(SoundcloudSearchResult sr, byte[] cover, String inPath, String outPath) {
try {
Mp3File mp3 = new Mp3File(inPath);
ID3Wrapper newId3Wrapper = new ID3Wrapper(new ID3v1Tag(), new ID3v23Tag());
newId3Wrapper.setAlbum(sr.getUsername() + ": " + sr.getDisplayName() + " via SoundCloud.com");
newId3Wrapper.setArtist(sr.getUsername());
newId3Wrapper.setTitle(sr.getDisplayName());
newId3Wrapper.setAlbumImage(cover, "image/jpg");
newId3Wrapper.setUrl(sr.getDetailsUrl());
newId3Wrapper.getId3v2Tag().setPadding(true);
mp3.setId3v1Tag(newId3Wrapper.getId3v1Tag());
mp3.setId3v2Tag(newId3Wrapper.getId3v2Tag());
mp3.save(outPath);
return true;
} catch (Throwable e) {
LOG.error("Error setting art information for soundcloud download", e);
return false;
}
}
use of org.jaudiotagger.tag.id3.ID3v1Tag in project UniversalMediaServer by UniversalMediaServer.
the class AudioUtils method parseRealAudio.
/**
* Parses the old RealAudio 1.0 and 2.0 formats that's not supported by
* neither {@link org.jaudiotagger} nor MediaInfo. Returns {@code false} if
* {@code channel} isn't one of these formats or the parsing fails.
* <p>
* Primary references:
* <ul>
* <li><a href="https://wiki.multimedia.cx/index.php/RealMedia">RealAudio on
* MultimediaWiki</a></li>
* <li><a
* href="https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/rmdec.c"
* >FFmpeg rmdec.c</a></li>
* </ul>
*
* @param channel the {@link Channel} containing the input. Size will only
* be parsed if {@code channel} is a {@link FileChannel}
* instance.
* @param media the {@link DLNAMediaInfo} instance to write the parsing
* results to.
* @return {@code true} if the {@code channel} input is in RealAudio 1.0 or
* 2.0 format and the parsing succeeds; false otherwise
*/
public static boolean parseRealAudio(ReadableByteChannel channel, DLNAMediaInfo media) {
final byte[] magicBytes = { 0x2E, 0x72, 0x61, (byte) 0xFD };
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.order(ByteOrder.BIG_ENDIAN);
DLNAMediaAudio audio = new DLNAMediaAudio();
try {
int count = channel.read(buffer);
if (count < 4) {
LOGGER.trace("Input is too short to be RealAudio");
return false;
}
buffer.flip();
byte[] signature = new byte[4];
buffer.get(signature);
if (!Arrays.equals(magicBytes, signature)) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Input signature ({}) mismatches RealAudio version 1.0 or 2.0", new String(signature, StandardCharsets.US_ASCII));
}
return false;
}
media.setContainer(FormatConfiguration.RA);
short version = buffer.getShort();
int reportedHeaderSize = 0;
int reportedDataSize = 0;
if (version == 3) {
audio.setCodecA(FormatConfiguration.REALAUDIO_14_4);
audio.getAudioProperties().setNumberOfChannels(1);
audio.getAudioProperties().setSampleFrequency(8000);
short headerSize = buffer.getShort();
buffer = ByteBuffer.allocate(headerSize);
channel.read(buffer);
buffer.flip();
buffer.position(8);
int bytesPerMinute = buffer.getShort() & 0xFFFF;
reportedDataSize = buffer.getInt();
byte b = buffer.get();
if (b != 0) {
byte[] title = new byte[b & 0xFF];
buffer.get(title);
String titleString = new String(title, StandardCharsets.US_ASCII);
audio.setSongname(titleString);
audio.setAudioTrackTitleFromMetadata(titleString);
}
b = buffer.get();
if (b != 0) {
byte[] artist = new byte[b & 0xFF];
buffer.get(artist);
audio.setArtist(new String(artist, StandardCharsets.US_ASCII));
}
audio.setBitRate(bytesPerMinute * 8 / 60);
media.setBitrate(bytesPerMinute * 8 / 60);
} else if (version == 4 || version == 5) {
buffer = ByteBuffer.allocate(14);
channel.read(buffer);
buffer.flip();
buffer.get(signature);
if (!".ra4".equals(new String(signature, StandardCharsets.US_ASCII))) {
LOGGER.debug("Invalid RealAudio 2.0 signature \"{}\"", new String(signature, StandardCharsets.US_ASCII));
return false;
}
reportedDataSize = buffer.getInt();
// skip version repeated
buffer.getShort();
reportedHeaderSize = buffer.getInt();
buffer = ByteBuffer.allocate(reportedHeaderSize);
channel.read(buffer);
buffer.flip();
// skip codec flavor
buffer.getShort();
// skip coded frame size
buffer.getInt();
// skip unknown
buffer.getInt();
long bytesPerMinute = buffer.getInt() & 0xFFFFFFFFL;
// skip unknown
buffer.getInt();
// skip sub packet
buffer.getShort();
// skip frame size
buffer.getShort();
// skip sub packet size
buffer.getShort();
// skip unknown
buffer.getShort();
if (version == 5) {
// skip unknown
buffer.position(buffer.position() + 6);
}
short sampleRate = buffer.getShort();
// skip unknown
buffer.getShort();
short sampleSize = buffer.getShort();
short nrChannels = buffer.getShort();
byte[] fourCC;
if (version == 4) {
// skip interleaver id
buffer.position(buffer.get() + buffer.position());
fourCC = new byte[buffer.get()];
buffer.get(fourCC);
} else {
// skip deinterlace id
buffer.getFloat();
fourCC = new byte[4];
buffer.get(fourCC);
}
String fourCCString = new String(fourCC, StandardCharsets.US_ASCII).toLowerCase(Locale.ROOT);
switch(fourCCString) {
case "lpcJ":
audio.setCodecA(FormatConfiguration.REALAUDIO_14_4);
break;
case "28_8":
audio.setCodecA(FormatConfiguration.REALAUDIO_28_8);
break;
case "dnet":
audio.setCodecA(FormatConfiguration.AC3);
break;
case "sipr":
audio.setCodecA(FormatConfiguration.SIPRO);
break;
case "cook":
audio.setCodecA(FormatConfiguration.COOK);
case "atrc":
audio.setCodecA(FormatConfiguration.ATRAC);
case "ralf":
audio.setCodecA(FormatConfiguration.RALF);
case "raac":
audio.setCodecA(FormatConfiguration.AAC_LC);
case "racp":
audio.setCodecA(FormatConfiguration.HE_AAC);
default:
LOGGER.debug("Unknown RealMedia codec FourCC \"{}\" - parsing failed", fourCCString);
return false;
}
if (buffer.hasRemaining()) {
parseRealAudioMetaData(buffer, audio, version);
}
audio.setBitRate((int) (bytesPerMinute * 8 / 60));
media.setBitrate((int) (bytesPerMinute * 8 / 60));
audio.setBitsperSample(sampleSize);
audio.getAudioProperties().setNumberOfChannels(nrChannels);
audio.getAudioProperties().setSampleFrequency(sampleRate);
} else {
LOGGER.error("Could not parse RealAudio format - unknown format version {}", version);
return false;
}
media.getAudioTracksList().add(audio);
long fileSize = 0;
if (channel instanceof FileChannel) {
fileSize = ((FileChannel) channel).size();
media.setSize(fileSize);
}
// Duration is estimated based on bitrate and might not be accurate
if (audio.getBitRate() > 0) {
int dataSize;
if (fileSize > 0 && reportedHeaderSize > 0) {
int fullHeaderSize = reportedHeaderSize + (version == 3 ? 8 : 16);
if (reportedDataSize > 0) {
dataSize = (int) Math.min(reportedDataSize, fileSize - fullHeaderSize);
} else {
dataSize = (int) (fileSize - fullHeaderSize);
}
} else {
dataSize = reportedDataSize;
}
media.setDuration((double) dataSize / audio.getBitRate() * 8);
}
} catch (IOException e) {
LOGGER.debug("Error while trying to parse RealAudio version 1 or 2: {}", e.getMessage());
LOGGER.trace("", e);
return false;
}
if (PMS.getConfiguration() != null && !PMS.getConfiguration().getAudioThumbnailMethod().equals(CoverSupplier.NONE) && (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname()) || StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist()))) {
ID3v1Tag tag = new ID3v1Tag();
if (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname())) {
tag.setTitle(media.getFirstAudioTrack().getSongname());
}
if (StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist())) {
tag.setArtist(media.getFirstAudioTrack().getArtist());
}
try {
media.setThumb(DLNAThumbnail.toThumbnail(CoverUtil.get().getThumbnail(tag), 640, 480, ScaleType.MAX, ImageFormat.SOURCE, false));
} catch (IOException e) {
LOGGER.error("An error occurred while generating thumbnail for RealAudio source: [\"{}\", \"{}\"]", tag.getFirstTitle(), tag.getFirstArtist());
}
}
media.setThumbready(true);
media.setMediaparsed(true);
return true;
}
use of org.jaudiotagger.tag.id3.ID3v1Tag in project MusicDNA by harjot-oberai.
the class AbstractLyrics3 method delete.
/**
* @param file
* @throws IOException
*/
public void delete(RandomAccessFile file) throws IOException {
long filePointer;
ID3v1Tag id3v1tag = new ID3v1Tag();
}
use of org.jaudiotagger.tag.id3.ID3v1Tag in project MusicDNA by harjot-oberai.
the class Lyrics3v1 method write.
/**
* @param file
* @throws IOException
*/
public void write(RandomAccessFile file) throws IOException {
String str;
int offset;
byte[] buffer;
ID3v1Tag id3v1tag;
id3v1tag = null;
delete(file);
file.seek(file.length());
buffer = new byte[lyric.length() + 11 + 9];
str = "LYRICSBEGIN";
for (int i = 0; i < str.length(); i++) {
buffer[i] = (byte) str.charAt(i);
}
offset = str.length();
str = ID3Tags.truncate(lyric, 5100);
for (int i = 0; i < str.length(); i++) {
buffer[i + offset] = (byte) str.charAt(i);
}
offset += str.length();
str = "LYRICSEND";
for (int i = 0; i < str.length(); i++) {
buffer[i + offset] = (byte) str.charAt(i);
}
offset += str.length();
file.write(buffer, 0, offset);
if (id3v1tag != null) {
id3v1tag.write(file);
}
}
Aggregations