What is the problem with this python 3 script

71 views Asked by At

I made a script for compressing videos

import ffmpeg
import subprocess

result = subprocess.run("C:\\ffmpeg\\bin\\ffmpeg.exe"['ffmpeg -i output.mp4 -b 800k output.mp4'])
print(result)

When I run this an error will come like this-

    result = subprocess.run("C:\\ffmpeg\\bin\\ffmpeg.exe"['ffmpeg -i output.mp4 -b 800k output.mp4'])
TypeError: string indices must be integers

What is the solution?

3

There are 3 answers

1
selbie On BEST ANSWER

This appears to be malformed:

subprocess.run("C:\\ffmpeg\\bin\\ffmpeg.exe"['ffmpeg -i output.mp4 -b 800k output.mp4'])

I'm pretty sure you just want this:

result = subprocess.run(["C:\\ffmpeg\\bin\\ffmpeg.exe", "-i", "output.mp4", "-b", "800k",  "output.mp4"])

Or perhaps just this:

result = subprocess.run("C:\\ffmpeg\\bin\\ffmpeg.exe -i output.mp4 -b 800k output.mp4")

Also, not sure if it's a good idea to have your input file and output file, output.mp4 be the same.

3
Prune On

Well, look at the argument you tried to pass. I've insert a line feed to help reading:

"C:\\ffmpeg\\bin\\ffmpeg.exe"
    ['ffmpeg -i output.mp4 -b 800k output.mp4']

The first line is a string. You followed that with a bracket. What comes next must be a subscript for the string. What did you think you were doing?

1
Abhinav Mathur On

"C:\\ffmpeg\\bin\\ffmpeg.exe"['ffmpeg -i output.mp4 -b 800k output.mp4'] As @Prune has pointed out, the error occurs as you're indexing the string using another string, which is incorrect. The correct way to pass arguments to subprocess.run is

result = subprocess.run("[C:\\ffmpeg\\bin\\ffmpeg.exe", 'ffmpeg -i output.mp4 -b 800k output.mp4']) If you want to add more arguments, you can do so using positional arguments.