How to compress a video using ffmpeg in python?

6.5k views Asked by At

I made this script for compressing videos in Python 3:

import os
import sys
import subprocess
result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
print(result)

When I run the above, some error will come up like System cannot find the file specified :

  result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
  File "C:\Program Files\Python37\lib\subprocess.py", line 488, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Program Files\Python37\lib\subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files\Python37\lib\subprocess.py", line 1207, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Question: How to fix code for correct compressing of videos?

1

There are 1 answers

7
武状元 Woa On

Nearly correct! It looks like the only module you need is subprocess. You should run your command in run() function. Try this:

import subprocess
result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
print(result)