I am trying to use yt-dlp command in my java code to extract audio from youtube videos. Basically, I am trying to develop one audio playing application(for better understanding on working with API) where I am using Youtube API. I search any music in my app and it will show youtube video in listview. At this time, audio should extracted from youtube vidoes as I am passing my command in updateListView method. It should return URL(storing in model) which will be used in other class files to play mp3 files. I am trying this thing for more than 1 week now. I tried several ways to do this thing but everytime I got only one same error which image I attached.
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;
}
I am trying to call this method in below mentioned method.
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
Error :
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)
So far I tried so many methods those I mentioned below:
What more I can try to extract audio from youtube video using yt-dlp command or Is there any other way to do this???
After surfing the internet for so many days, I finally found out the solution to do this feature.Well, you can't directly add command code in your android studio java code, as of my research. It will keep throwing an error that yt_dlp: No such or directory as code can't find the path of yt_dlp even though if you add this .exe file of yt_dlp in your assest folder. The simple solution is, as yt_dlp is python module, so we can integrate our python code into our android studio which is allowed to do so, we can get the same result as we got from command line. For that we need to add python library in android studio which is you can get it from here. You can get that python code as well from my repository here.It is located under python folder. This code will return you audio-extracted url which can be used further to play audio file. Hardwork Pays!!!Keep Coding!