I am using pytube to stream a video in Android, with the help of chaquopy.
videofile.py
from pytube import YouTube
def video(link):
yt = YouTube(f'https://www.youtube.com/watch?v=' + link)
stream_url = yt.streams.get_highest_resolution().url
return stream_url
VideoActivityPy.java
progressBar = findViewById(R.id.pro);
videoView = findViewById(R.id.videoview);
new Thread(() -> {
try {
if (!Python.isStarted()) {
Python.start(new AndroidPlatform(VideoActivityPy.this));
}
python = Python.getInstance();
pyScript = python.getModule("videofile");
videoUri = pyScript.callAttr("video", MyData.videoLink);
runOnUiThread(() -> {
videoView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
Uri uri = Uri.parse(videoUri.toString());
videoView.setVideoURI(uri);
MediaController mediaController = new MediaController(VideoActivityPy.this);
mediaController.setAnchorView(videoView);
mediaController.setMediaPlayer(videoView);
videoView.setMediaController(mediaController);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
progressBar.setVisibility(View.INVISIBLE);
videoView.start();
}
});
});
}
catch (com.chaquo.python.PyException pyException) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(VideoActivityPy.this, "Check your internet connection", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(VideoActivityPy.this, e.toString(), Toast.LENGTH_LONG).show();
}
}).start();
At first, I had written the code without using Thread, but the app was not responding. So, I used Thread. Now the app works, the video loads, but it takes about 40-50 seconds to start the video (the video is 1.5 hours long though). Is there any way to reduce the loading time?
Note: I have downloaded .tar.gz file from PyPI, changed the built-in code of pytube, and then written the gradle as below:
python {
buildPython "C:/Python38/python.exe"
pip {
install "pytube-15.0.0.tar.gz"
}
}
I changed the var_regex in cipher.py