From c5243ade5647b6a788b6c0a318863caad57a18f7 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:52:20 -0400 Subject: Fix Reddit video downloaded without sound. --- .../services/DownloadRedditVideoService.java | 35 +++++++++++++++++----- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java index 43565dd7..0fbaf4c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java @@ -26,6 +26,8 @@ import android.os.Message; import android.os.Process; import android.provider.MediaStore; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -83,6 +85,7 @@ public class DownloadRedditVideoService extends Service { private ServiceHandler serviceHandler; private NotificationManagerCompat notificationManager; private NotificationCompat.Builder builder; + private final String[] possibleAudioUrlSuffices = new String[]{"/DASH_AUDIO_128.mp4", "/DASH_audio.mp4", "/DASH_audio", "/audio.mp4", "/audio"}; public DownloadRedditVideoService() { } @@ -95,7 +98,9 @@ public class DownloadRedditVideoService extends Service { public void handleMessage(Message msg) { Bundle intent = msg.getData(); String videoUrl = intent.getString(EXTRA_VIDEO_URL); - String audioUrl = Build.VERSION.SDK_INT > Build.VERSION_CODES.N ? videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4" : null; + + String audioUrlPrefix = Build.VERSION.SDK_INT > Build.VERSION_CODES.N ? videoUrl.substring(0, videoUrl.lastIndexOf('/')) : null; + String subredditName = intent.getString(EXTRA_SUBREDDIT); String fileNameWithoutExtension = subredditName + "-" + intent.getString(EXTRA_POST_ID); boolean isNsfw = intent.getBoolean(EXTRA_IS_NSFW, false); @@ -129,7 +134,7 @@ public class DownloadRedditVideoService extends Service { retrofit = retrofit.newBuilder().client(client).build(); - DownloadFile downloadFile = retrofit.create(DownloadFile.class); + DownloadFile downloadFileRetrofit = retrofit.create(DownloadFile.class); boolean separateDownloadFolder = sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false); @@ -138,7 +143,7 @@ public class DownloadRedditVideoService extends Service { String destinationFileName = fileNameWithoutExtension + ".mp4"; try { - Response videoResponse = downloadFile.downloadFile(videoUrl).execute(); + Response videoResponse = downloadFileRetrofit.downloadFile(videoUrl).execute(); if (videoResponse.isSuccessful() && videoResponse.body() != null) { String externalCacheDirectoryPath = externalCacheDirectory.getAbsolutePath() + "/"; String destinationFileDirectory; @@ -218,13 +223,13 @@ public class DownloadRedditVideoService extends Service { return; } - if (audioUrl != null) { - Response audioResponse = downloadFile.downloadFile(audioUrl).execute(); + if (audioUrlPrefix != null) { + ResponseBody audioResponse = getAudioResponse(downloadFileRetrofit, audioUrlPrefix, 0); String outputFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + ".mp4"; - if (audioResponse.isSuccessful() && audioResponse.body() != null) { + if (audioResponse != null) { String audioFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + "-cache.mp3"; - String savedAudioFilePath = writeResponseBodyToDisk(audioResponse.body(), audioFilePath); + String savedAudioFilePath = writeResponseBodyToDisk(audioResponse, audioFilePath); if (savedAudioFilePath == null) { downloadFinished(null, ERROR_AUDIO_FILE_CANNOT_SAVE, randomNotificationIdOffset); return; @@ -298,6 +303,22 @@ public class DownloadRedditVideoService extends Service { } } + @Nullable + private ResponseBody getAudioResponse(DownloadFile downloadFileRetrofit, @NonNull String audioUrlPrefix, int audioSuffixIndex) throws IOException { + if (audioSuffixIndex >= possibleAudioUrlSuffices.length) { + return null; + } + + String audioUrl = audioUrlPrefix + possibleAudioUrlSuffices[audioSuffixIndex]; + Response audioResponse = downloadFileRetrofit.downloadFile(audioUrl).execute(); + ResponseBody responseBody = audioResponse.body(); + if (audioResponse.isSuccessful() && responseBody != null) { + return responseBody; + } + + return getAudioResponse(downloadFileRetrofit, audioUrlPrefix, audioSuffixIndex + 1); + } + private String writeResponseBodyToDisk(ResponseBody body, String filePath) { try { File file = new File(filePath); -- cgit v1.2.3