How do I use ffmpeg library in Python on Android with Pydroid?

Question:

I’m trying to run the following code that uses the ffmpeg library on the app Pydroid 3:

import subprocess 

# Video path 
video_path = '/storage/emulated/0/Download/video.mp4' 

# Output SRT file path 
srt_path = '/storage/emulated/0/Download/subtitles.srt' 

# Run ffmpeg command 
subprocess.run(['ffmpeg', '-i', video_path, '-map', '0:s:0', srt_path])

But I receive the message:

Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(),  __main__.__dict__)
File "<string>", line 10, in <module>
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/subprocess.py", line 953, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg'

I’ve already installed the ffmpeg library using pip. I tried installing ffmpeg-python, python-ffmpeg and ffmpegio, but I kept getting errors. Then I installed ffmpeg using Termux and specified the ffmpeg path in the code, like this:

subprocess.run(['/data/data/com.termux/files/usr/bin/ffmpeg', '-i', video_path, '-map', '0:s:0', srt_path])

But I got the message:

PermissionError: [Errno 13] Permission denied: '/data/data/com.termux/files/usr/bin/ffmpeg'

My phone is running Android 11 and it’s not rooted. I’m a complete beginner and have no idea how to fix this.

Asked By: Cristina T.

||

Answers:

If you just want to experiment with ffmpeg commands or edit your videos with it you can use ffmpeg app, it’s available on Play Store, Anything else you will have to install ffmpeg first on android itself not as an app because even if you install it as a python module it will need ffmpeg to be installed on the system to run, to install it on Android your phone must be rooted, the better and the simple way to run your code is using Termux and install ffmpeg and python on it, here is how:

First install Termux apk from f-droid Termux (don’t install it from play store, you will face problems)

Open the app and run
pkg update && upgrade

Then pkg install python3

Then pkg install ffmpeg

Install nano too to create your python file easier

pkg install nano

then run nano myfile.py to create your python file, a window will popup past your script/code in it then press CTRL + x and press y to save your script as myfile.py

To run your script just type python myfile.py and your script will run.

I hope that helps you.

Answered By: TheRedM
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.