Skip to content

Commit

Permalink
Fix Reddit video downloaded without sound.
Browse files Browse the repository at this point in the history
  • Loading branch information
Docile-Alligator committed Aug 13, 2023
1 parent d0a9d9a commit c5243ad
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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;
Expand Down Expand Up @@ -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() {
}
Expand All @@ -95,7 +98,9 @@ public ServiceHandler(Looper looper) {
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);
Expand Down Expand Up @@ -129,7 +134,7 @@ public void handleMessage(Message msg) {

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);

Expand All @@ -138,7 +143,7 @@ public void handleMessage(Message msg) {
String destinationFileName = fileNameWithoutExtension + ".mp4";

try {
Response<ResponseBody> videoResponse = downloadFile.downloadFile(videoUrl).execute();
Response<ResponseBody> videoResponse = downloadFileRetrofit.downloadFile(videoUrl).execute();
if (videoResponse.isSuccessful() && videoResponse.body() != null) {
String externalCacheDirectoryPath = externalCacheDirectory.getAbsolutePath() + "/";
String destinationFileDirectory;
Expand Down Expand Up @@ -218,13 +223,13 @@ public void handleMessage(Message msg) {
return;
}

if (audioUrl != null) {
Response<ResponseBody> 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;
Expand Down Expand Up @@ -298,6 +303,22 @@ public void handleMessage(Message msg) {
}
}

@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<ResponseBody> 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);
Expand Down

0 comments on commit c5243ad

Please sign in to comment.