Merge pull request #5769 from ByteHamster/id3-comment

Extract shownotes from id3 comment field
This commit is contained in:
ByteHamster 2022-03-10 07:14:48 +01:00 committed by GitHub
commit 36a36e4f85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -4,11 +4,13 @@ import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.documentfile.provider.DocumentFile;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -34,8 +36,12 @@ import de.danoeh.antennapod.model.feed.FeedMedia;
import de.danoeh.antennapod.model.feed.FeedPreferences;
import de.danoeh.antennapod.model.playback.MediaType;
import de.danoeh.antennapod.parser.feed.util.MimeTypeUtils;
import de.danoeh.antennapod.parser.media.id3.ID3ReaderException;
import de.danoeh.antennapod.parser.media.id3.Id3MetadataReader;
import org.apache.commons.io.input.CountingInputStream;
public class LocalFeedUpdater {
private static final String TAG = "LocalFeedUpdater";
static final String[] PREFERRED_FEED_IMAGE_FILENAMES = { "folder.jpg", "Folder.jpg", "folder.png", "Folder.png" };
@ -197,6 +203,15 @@ public class LocalFeedUpdater {
item.getMedia().setDuration((int) Long.parseLong(durationStr));
item.getMedia().setHasEmbeddedPicture(mediaMetadataRetriever.getEmbeddedPicture() != null);
try (InputStream inputStream = context.getContentResolver().openInputStream(file.getUri())) {
Id3MetadataReader reader = new Id3MetadataReader(new CountingInputStream(inputStream));
reader.readInputStream();
item.setDescriptionIfLonger(reader.getComment());
} catch (IOException | ID3ReaderException e) {
// Do not flood Logcat with full stack traces
Log.d(TAG, "Unable to parse ID3 of " + file.getUri() + ": " + e.getMessage());
}
}
private static void reportError(Feed feed, String reasonDetailed) {

View File

@ -0,0 +1,39 @@
package de.danoeh.antennapod.parser.media.id3;
import androidx.annotation.NonNull;
import de.danoeh.antennapod.parser.media.id3.model.FrameHeader;
import org.apache.commons.io.input.CountingInputStream;
import java.io.IOException;
/**
* Reads general ID3 metadata like comment, which Android's MediaMetadataReceiver does not support.
*/
public class Id3MetadataReader extends ID3Reader {
public static final String FRAME_ID_COMMENT = "COMM";
private String comment = null;
public Id3MetadataReader(CountingInputStream input) {
super(input);
}
@Override
protected void readFrame(@NonNull FrameHeader frameHeader) throws IOException, ID3ReaderException {
if (FRAME_ID_COMMENT.equals(frameHeader.getId())) {
long frameStart = getPosition();
int encoding = readByte();
skipBytes(3); // Language
String shortDescription = readEncodedString(encoding, frameHeader.getSize() - 4);
String longDescription = readEncodedString(encoding,
(int) (frameHeader.getSize() - (getPosition() - frameStart)));
comment = shortDescription.length() > longDescription.length() ? shortDescription : longDescription;
} else {
super.readFrame(frameHeader);
}
}
public String getComment() {
return comment;
}
}