-1

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 Answers3

1

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.

selbie
  • 100,020
  • 15
  • 103
  • 173
0

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?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • I can't; I don't know what you think this is supposed to be. Two separate arguments, perhaps? If so, a comma corrects the problem -- and we close the question as a typo. – Prune Oct 21 '20 at 05:27
  • Then also it is showing - `result = subprocess.run("C:\\ffmpeg\\bin\\ffmpeg.exe"['ffmpeg -i output.mp4 -b 800k output.mp4']) TypeError: string indices must be integers` –  Oct 21 '20 at 05:33
  • "Then" what? The code in your comment is essentially identical to the one in your post. Did you try the separate arguments I suggested? – Prune Oct 21 '20 at 05:42
0

"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.

Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24