我正在尝试在我的java代码中使用 yt-dlp 命令从 youtube 视频中提取音频。基本上,我正在尝试开发一个音频播放应用程序(为了更好地理解使用 API),我正在使用 Youtube API。我在我的应用程序中搜索任何音乐,它会在列表视图中显示 YouTube 视频。此时,当我在 updateListView 方法中传递命令时,应该从 YouTube 视频中提取音频。它应该返回 URL(存储在模型中),该 URL 将在其他类文件中用于播放 mp3 文件。 我已经尝试这件事超过 1 周了。我尝试了几种方法来做到这一点,但每次我都只得到一个相同的错误,我附上了图片。
private String getAudioUrl(String videoId)
{
File cacheDir = getCacheDir();
File audioDir = new File(cacheDir, "audio");
if (!audioDir.exists())
{
audioDir.mkdirs(); // Create the directory if it doesn't exist
}
try {
ProcessBuilder processBuilder = new ProcessBuilder(
"~/.yt-dlp",
"-x",
"--audio-format", "mp3",
"-P", audioDir.getAbsolutePath() + "/%(title)s.%(ext)s",
"https://www.youtube.com/watch?v=" + videoId
);
Process process = processBuilder.start();
BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()));
String audioUrl = read.readLine();
read.close();
Toast.makeText(MainActivity.this, "Playing!!!", Toast.LENGTH_SHORT).show();
return audioUrl;
} catch (IOException e)
{
e.printStackTrace();
Toast.makeText(MainActivity.this, "Sorry, Error in playing audio!!!", Toast.LENGTH_SHORT).show();
}
return null;
}
我正在尝试在下面提到的方法中调用此方法。
private void updateSearchResultsList(List<SearchResult> results)
{
searchResults.clear();
for (SearchResult result : results)
{
String videoTitle = result.getSnippet().getTitle();
String videoImageUrl = result.getSnippet().getThumbnails().getDefault().getUrl();
selectedVideoId = result.getId().getVideoId();
//for testing
// Log.d("Video ID", "Video ID: " + videoId);
String audioUrl = getAudioUrl(selectedVideoId);
YoutubeModel youtubeModel = new YoutubeModel(videoTitle, videoImageUrl, audioUrl);
searchResults.add(youtubeModel);
}
adapter.notifyDataSetChanged();
}//end of method
错误:
java.io.IOException: Cannot run program "~/.yt-dlp": error=2, No such file or directory
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at java.lang.ProcessBuilder.start(ProcessBuilder.java:1050)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at com.example.audio_player.Activity.MainActivity.getAudioUrl(MainActivity.java:196)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at com.example.audio_player.Activity.MainActivity.updateSearchResultsList(MainActivity.java:170)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at com.example.audio_player.Activity.MainActivity.access$400(MainActivity.java:31)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at com.example.audio_player.Activity.MainActivity$YouTubeSearchTask.onPostExecute(MainActivity.java:146)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at com.example.audio_player.Activity.MainActivity$YouTubeSearchTask.onPostExecute(MainActivity.java:121)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at android.os.AsyncTask.finish(AsyncTask.java:771)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at android.os.AsyncTask.-$$Nest$mfinish(Unknown Source:0)
2024-05-06 18:19:45.118 24092-24092 System.err com.example.audio_player W at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at android.os.Handler.dispatchMessage(Handler.java:106)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at android.os.Looper.loopOnce(Looper.java:230)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at android.os.Looper.loop(Looper.java:319)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at android.app.ActivityThread.main(ActivityThread.java:8919)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at java.lang.reflect.Method.invoke(Native Method)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W Caused by: java.io.IOException: error=2, No such file or directory
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at java.lang.UNIXProcess.forkAndExec(Native Method)
2024-05-06 18:19:45.119 24092-24092 System.err com.example.audio_player W at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
2024-05-06 18:19:45.120 24092-24092 System.err com.example.audio_player W at java.lang.ProcessImpl.start(ProcessImpl.java:141)
2024-05-06 18:19:45.120 24092-24092 System.err com.example.audio_player W at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
到目前为止,我尝试了很多下面提到的方法:
我还可以尝试使用 yt-dlp 命令从 youtube 视频中提取音频,或者还有其他方法可以做到这一点吗???
在网上冲浪了这么多天,我终于找到了实现此功能的解决方案。嗯,根据我的研究,你不能直接在你的android studio java代码中添加命令代码。即使您在 asset 文件夹中添加了 yt_dlp 的 .exe 文件,它也会不断抛出错误 yt_dlp: No such or directory as code can't find the path of yt_dlp 。 简单的解决方案是,由于 yt_dlp 是 python 模块,因此我们可以将 python 代码集成到允许这样做的 android studio 中,我们可以获得与从命令行获得相同的结果。为此,我们需要在 android studio 中添加 python 库,您可以从here获取它。您也可以从我的存储库here获取该Python代码。它位于Python文件夹下。此代码将返回音频提取的 url,可用于进一步播放音频文件。努力是有回报的!!!继续编码!