0

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?

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • 1
    Have you tried it yourself? – Abhinav Mathur Oct 19 '20 at 05:06
  • Yes I have tried it and it will show invalid syntax –  Oct 19 '20 at 05:30
  • 1
    What error are you facing? – yudhiesh Oct 19 '20 at 06:00
  • 1
    These commands are supposed to be run in the terminal instead of a script, as far as I know – Abhinav Mathur Oct 19 '20 at 06:11
  • 1
    If you want to use commands like `-i input.mp4` syntax then you need to run `ffmpeg.exe` as a Python **subProcess**. See if this [Answer](https://stackoverflow.com/a/25964123/2057709) helps you (replace their `+str(out_movie),` with your `+'output.mp4',`) – VC.One Oct 19 '20 at 07:42
  • There is a library ffmpeg (https://pypi.org/project/ffmpeg-python/). API reference https://kkroening.github.io/ffmpeg-python/ – Krishna Vaddepalli Oct 19 '20 at 07:45
  • @yuRa It is showing a error like input is invalid syntax –  Oct 19 '20 at 09:10
  • thanks @VC.One. but it is showing -i is not defined –  Oct 19 '20 at 09:30
  • 1
    Show us your `subprocess.call` line. PS: You can use the [edit](https://stackoverflow.com/posts/64421177/edit) button (under the tags) to add new info. Show simple and testable code for us to recreate your same problem. – VC.One Oct 19 '20 at 12:25

1 Answers1

3

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)
武状元 Woa
  • 690
  • 3
  • 10
  • 24
  • thanks but it is showing an error like this ``` 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``` –  Oct 20 '20 at 06:55
  • 1
    It looks like you don't have ffmpeg installed. Follow this! http://blog.gregzaal.com/how-to-install-ffmpeg-on-windows/ – 武状元 Woa Oct 20 '20 at 07:31
  • @ 武状元 I have installed ffmpeg-python using pip install python-ffmpeg –  Oct 20 '20 at 08:50
  • 1
    Not finished! You need to install FFmpeg. ffmpeg-python is just a Python wrapper for FFmpeg. – 武状元 Woa Oct 20 '20 at 09:01
  • Also I have installed ffmpeg.exe file –  Oct 20 '20 at 09:06
  • 1
    Type `ffmpeg` in windows shell. How it says? – 武状元 Woa Oct 20 '20 at 09:09
  • Ok I will try @武状元 –  Oct 20 '20 at 09:24