Skip to content

Commit 6d9c8c1

Browse files
committed
Crop YouTube Music album covers
1 parent 5fa8895 commit 6d9c8c1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

app/src/main/java/us/shandian/giga/get/DownloadMission.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import us.shandian.giga.util.Utility;
4242

4343
import static org.schabi.newpipe.BuildConfig.DEBUG;
44+
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
4445

4546
public class DownloadMission extends Mission {
4647
private static final long serialVersionUID = 6L;// last bump: 07 october 2019
@@ -866,12 +867,25 @@ public void fetchThumbnail(@NonNull final List<Image> images) {
866867
final String thumbnailUrl = ImageStrategy.choosePreferredImage(
867868
images, PreferredImageQuality.HIGH);
868869
// TODO: get context from somewhere else
869-
thumbnail = CoilHelper.INSTANCE.loadBitmapBlocking(App.getInstance(), thumbnailUrl);
870+
Bitmap originalThumbnail = CoilHelper.INSTANCE.loadBitmapBlocking(
871+
App.getInstance(), thumbnailUrl);
872+
873+
// YouTube Music streams have non square thumbnails to fit the player aspect ratio
874+
// of 16:9. We can safely crop the thumbnail to a square because the squared thumbnail
875+
// is padded with bars on the sides.
876+
if (originalThumbnail != null && streamInfo.getService().equals(YouTube)
877+
&& streamInfo.getSongMetadata() != null // i.e. YT Music stream
878+
&& originalThumbnail.getWidth() > originalThumbnail.getHeight()) {
879+
int cropSize = Math.min(originalThumbnail.getWidth(), originalThumbnail.getHeight());
880+
int xOffset = (originalThumbnail.getWidth() - cropSize) / 2;
881+
originalThumbnail = Bitmap.createBitmap(originalThumbnail, xOffset, 0,
882+
cropSize, cropSize);
883+
}
884+
this.thumbnail = originalThumbnail;
870885
thumbnailFetched = true;
871886
} catch (final Exception e) {
872887
Log.w(TAG, "fetchThumbnail: failed to load thumbnail", e);
873888
thumbnailFetched = true;
874-
return;
875889
}
876890
}
877891

0 commit comments

Comments
 (0)