Skip to content

Commit

Permalink
Merge pull request #176 from xibr/set-ffmpeg-location
Browse files Browse the repository at this point in the history
Set ffmpeg location and change yt-dlp update method
  • Loading branch information
xibr authored Aug 15, 2022
2 parents 350688f + 8f77dd9 commit 1c3804d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public class YoutubeDL {
private static final String pythonLibName = "libpython.zip.so";
private static final String pythonDirName = "python";
private static final String ffmpegDirName = "ffmpeg";
protected static final String youtubeDLDirName = "yt-dlp";
private static final String youtubeDLBin = "__main__.py";
protected static final String youtubeDLFile = "yt_dlp.zip";
private static final String ffmpegBinName = "libffmpeg.bin.so";
protected static final String ytdlpDirName = "yt-dlp";
protected static final String ytdlpBin = "yt-dlp";
private static final String pythonLibVersion = "pythonLibVersion";

private boolean initialized = false;
private File pythonPath;
private File youtubeDLPath;
private File ffmpegPath;
private File ytdlpPath;
private File binDir;

private String ENV_LD_LIBRARY_PATH;
Expand All @@ -66,29 +67,34 @@ synchronized public void init(Context appContext) throws YoutubeDLException {
File packagesDir = new File(baseDir, packagesRoot);
binDir = new File(appContext.getApplicationInfo().nativeLibraryDir);
pythonPath = new File(binDir, pythonBinName);
ffmpegPath = new File(binDir, ffmpegBinName);
File pythonDir = new File(packagesDir, pythonDirName);
File ffmpegDir = new File(packagesDir, ffmpegDirName);

File youtubeDLDir = new File(baseDir, youtubeDLDirName);
youtubeDLPath = new File(youtubeDLDir, youtubeDLBin);
File ytdlpDir = new File(baseDir, ytdlpDirName);
ytdlpPath = new File(ytdlpDir, ytdlpBin);

ENV_LD_LIBRARY_PATH = pythonDir.getAbsolutePath() + "/usr/lib" + ":" + ffmpegDir.getAbsolutePath() + "/usr/lib";
ENV_SSL_CERT_FILE = pythonDir.getAbsolutePath() + "/usr/etc/tls/cert.pem";
ENV_PYTHONHOME = pythonDir.getAbsolutePath() + "/usr";

initPython(appContext, pythonDir);
initYoutubeDL(appContext, youtubeDLDir);
init_ytdlp(appContext, ytdlpDir);

initialized = true;
}

protected void initYoutubeDL(Context appContext, File youtubeDLDir) throws YoutubeDLException {
if (!youtubeDLDir.exists()) {
youtubeDLDir.mkdirs();
protected void init_ytdlp(@NonNull final Context appContext, @NonNull final File ytdlpDir) throws YoutubeDLException {
if (!ytdlpDir.exists())
ytdlpDir.mkdirs();

final File ytdlpBinary = new File(ytdlpDir, ytdlpBin);
if (!ytdlpBinary.exists()) {
try {
ZipUtils.unzip(appContext.getResources().openRawResource(R.raw.yt_dlp), youtubeDLDir);
} catch (Exception e) {
FileUtils.deleteQuietly(youtubeDLDir);
final InputStream inputStream = appContext.getResources().openRawResource(R.raw.ytdlp); /* will be renamed to yt-dlp */
FileUtils.copyInputStreamToFile(inputStream, ytdlpBinary);
} catch (final Exception e) {
FileUtils.deleteQuietly(ytdlpDir);
throw new YoutubeDLException("failed to initialize", e);
}
}
Expand Down Expand Up @@ -188,6 +194,9 @@ public YoutubeDLResponse execute(YoutubeDLRequest request, @Nullable String proc
request.addOption("--no-cache-dir");
}

/* Set ffmpeg location, See https://github.com/xibr/ytdlp-lazy/issues/1 */
request.addOption("--ffmpeg-location", ffmpegPath.getAbsolutePath());

YoutubeDLResponse youtubeDLResponse;
Process process;
int exitCode;
Expand All @@ -197,7 +206,7 @@ public YoutubeDLResponse execute(YoutubeDLRequest request, @Nullable String proc

List<String> args = request.buildCommand();
List<String> command = new ArrayList<>();
command.addAll(Arrays.asList(pythonPath.getAbsolutePath(), youtubeDLPath.getAbsolutePath()));
command.addAll(Arrays.asList(pythonPath.getAbsolutePath(), ytdlpPath.getAbsolutePath()));
command.addAll(args);

ProcessBuilder processBuilder = new ProcessBuilder(command);
Expand Down Expand Up @@ -268,6 +277,6 @@ public String version(Context appContext) {
}

public enum UpdateStatus {
DONE, ALREADY_UP_TO_DATE;
DONE, ALREADY_UP_TO_DATE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.yausername.youtubedl_android.YoutubeDL.UpdateStatus;
import com.yausername.youtubedl_common.SharedPrefsHelper;
import com.yausername.youtubedl_common.utils.ZipUtils;

import org.apache.commons.io.FileUtils;

Expand All @@ -22,28 +21,29 @@ class YoutubeDLUpdater {
private YoutubeDLUpdater() {
}

private static final String releasesUrl = "https://api.github.com/repos/xibr/ytdlp-lazy/releases/latest";
private static final String releasesUrl = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest";
private static final String youtubeDLVersionKey = "youtubeDLVersion";

static UpdateStatus update(Context appContext) throws IOException, YoutubeDLException {
JsonNode json = checkForUpdate(appContext);
if(null == json) return UpdateStatus.ALREADY_UP_TO_DATE;
if (null == json) return UpdateStatus.ALREADY_UP_TO_DATE;

String downloadUrl = getDownloadUrl(json);
File file = download(appContext, downloadUrl);
File ytdlpDir = getYoutubeDLDir(appContext);
File binary = new File(ytdlpDir, "yt-dlp");

File youtubeDLDir = null;
try {
youtubeDLDir = getYoutubeDLDir(appContext);
//purge older version
FileUtils.deleteDirectory(youtubeDLDir);
//install newer version
youtubeDLDir.mkdirs();
ZipUtils.unzip(file, youtubeDLDir);
} catch (Exception e) {
//if something went wrong restore default version
FileUtils.deleteQuietly(youtubeDLDir);
YoutubeDL.getInstance().initYoutubeDL(appContext, youtubeDLDir);
/* purge older version */
if (ytdlpDir.exists())
FileUtils.deleteDirectory(ytdlpDir);
/* install newer version */
ytdlpDir.mkdirs();
FileUtils.copyFile(file, binary);
} catch (final Exception e) {
/* if something went wrong restore default version */
FileUtils.deleteQuietly(ytdlpDir);
YoutubeDL.getInstance().init_ytdlp(appContext, ytdlpDir);
throw new YoutubeDLException(e);
} finally {
file.delete();
Expand All @@ -62,13 +62,13 @@ private static JsonNode checkForUpdate(Context appContext) throws IOException {
JsonNode json = YoutubeDL.objectMapper.readTree(url);
String newVersion = getTag(json);
String oldVersion = SharedPrefsHelper.get(appContext, youtubeDLVersionKey);
if(newVersion.equals(oldVersion)){
if (newVersion.equals(oldVersion)) {
return null;
}
return json;
}

private static String getTag(JsonNode json){
private static String getTag(JsonNode json) {
return json.get("tag_name").asText();
}

Expand All @@ -77,7 +77,7 @@ private static String getDownloadUrl(@NonNull JsonNode json) throws YoutubeDLExc
ArrayNode assets = (ArrayNode) json.get("assets");
String downloadUrl = "";
for (JsonNode asset : assets) {
if (YoutubeDL.youtubeDLFile.equals(asset.get("name").asText())) {
if (YoutubeDL.ytdlpBin.equals(asset.get("name").asText())) {
downloadUrl = asset.get("browser_download_url").asText();
break;
}
Expand All @@ -89,15 +89,15 @@ private static String getDownloadUrl(@NonNull JsonNode json) throws YoutubeDLExc
@NonNull
private static File download(Context appContext, String url) throws IOException {
URL downloadUrl = new URL(url);
File file = File.createTempFile("yt_dlp", "zip", appContext.getCacheDir());
File file = File.createTempFile("yt-dlp", null, appContext.getCacheDir());
FileUtils.copyURLToFile(downloadUrl, file, 5000, 10000);
return file;
}

@NonNull
private static File getYoutubeDLDir(Context appContext) {
File baseDir = new File(appContext.getNoBackupFilesDir(), YoutubeDL.baseName);
return new File(baseDir, YoutubeDL.youtubeDLDirName);
return new File(baseDir, YoutubeDL.ytdlpDirName);
}

@Nullable
Expand Down
Binary file removed library/src/main/res/raw/yt_dlp.zip
Binary file not shown.
Binary file added library/src/main/res/raw/ytdlp
Binary file not shown.

0 comments on commit 1c3804d

Please sign in to comment.