MainActivity.java отлично запускает скрипт Python, единственная проблема заключается в том, что библиотека yt_dlp, которую я использовал, не имеет доступа к FFmpeg.Двоичный файл FFmpeg (для linuxarm64, я не думаю, что это проблема) находится в папке ресурсов (src/main/assets), затем копируется в /data/user/0/com.app.name /файлы:
Код: Выделить всё
//FileUtils.java
public class FileUtils {
public static void copyAsset(Context context, String assetName, String outputPath) {
AssetManager assetManager = context.getAssets();
try {
InputStream in = assetManager.open(assetName);
File outFile = new File(outputPath);
OutputStream out = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.close();
// Set executable permissions for the binary
outFile.setExecutable(true); // Ensure the file is executable
outFile.setReadable(true);
outFile.setWritable(true);
Log.d("FileUtils", "FFmpeg File - Exists: " + outFile.exists() +
", Executable: " + outFile.canExecute());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private pyRun pyrun;
private static final int REQUEST_CODE_PERMISSION = 123;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("YTDownloader", "onCreate: Starting...");
String ffmpegPath = getFilesDir() + "/ffmpeg";
// Copy the binary
FileUtils.copyAsset(this, "ffmpeg", ffmpegPath);
// Check if file exist and log
Log.d(TAG, "FFmpeg exists: " + new File(ffmpegPath).exists());
Log.d(TAG, "PATH: "+ffmpegPath);
//load python
//load webview
}
//other functions
}
Код: Выделить всё
import os
import yt_dlp as youtube_dl
ffmpeg_path='/data/user/0/com.dudek3d.ytdownloader/files/ffmpeg'
ydl_opts = {
'ffmpeg_location': ffmpeg_path,
'format': 'bestvideo+bestaudio/best',
'postprocessors': [{
'key': 'FFmpegVideoConvertor',
'preferedformat': 'mp4'}],
'noplaylist': True,
'quiet': True,
'merge_output_format': 'mp4',
'outtmpl': filename+'.%(ext)s',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
Я обнаружил, что доступ к файлу ffmpeg невозможен ни из Java, ни из Python.
Я пробовал:
1)
Изменить пути к кешу: ffmpeg_path='/data/user/0/com.dudek3d.ytdownloader/cache/ ffmpeg'
Изменение путей к данным: ffmpeg_path='/data/data/com.dudek3d.ytdownloader/files/ffmpeg'
Изменение путей к кэшу данных: ffmpeg_path ='/data/data/com.dudek3d.ytdownloader/cache/ffmpeg'
2)
Доступ к пути ffmpeg из MainActivity.java и получение сообщения «Разрешение отклонено» " Ошибка, поэтому я добавил
Код: Выделить всё
3)
Использование реализаций ffmpeg в зависимостях в build.gradle (приложение Module), например: 'com.arthenica:ffmpeg-kit-full:4.5'
но это не сработает, потому что я думаю, что это доступно через Java, а не через Python.
4)
Я изменил местоположение ffmpeg на src/main/jniLibs/arc64-v8a:
Код: Выделить всё
ffmpegPath = getApplicationInfo().nativeLibraryDir + "/ffmpeg";
Код: Выделить всё
def run(format_choice, listt, ffmpeg_pat, ffprobe_pat):
global ffmpeg_path, ffprobe_path
ffmpeg_path, ffprobe_path= ffmpeg_pat, ffprobe_pat
Код: Выделить всё
WARNING: ffmpeg-location /data/app/~~42sSKOephY26jjdLYW7wmw==/com.dudek3d.ytdownloader-OWqWbazkZobYNlsqTsxfyA==/lib/x86_64/ffmpeg does not exist!
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-android