Python subprocess or os.system not working with ffmpeg

45 Views Asked by At

I've been trying to get this darn programming run since last night and cannot seem to get anything to work. I want to first trim the video and then resize the video. I'm reading which video and what to give the final name from a text file, over 780 lines long, quite a few videos.

Thus far with every idea under the sun I have tried, subprocess and os.system, I can't get anything more than error statements or right now all I get is no file creation of any kind. How the heck do I get this to work correctly?

import ffmpeg
import subprocess
import os

os.chdir('/home/Downloads/SRs/')
a = open('SRt.txt', 'r')
b = a.readlines()
a.close()
for c in range(0, len(b)-1):
    words = list(b[c].split(" "))
    d = len(words)
    e = words[d-1]
    f = b[c].replace(e, 'FR' + str(c) + '.mp4')
    words[d-1] = 'FR' + str(c) + '.mp4'
    print(f)
    subprocess.call(f, shell=True)
    subprocess.call([
        'ffmpeg',
        '-i',
        "'FR' + str(c) + '.mp4'",
        '-vf scale=320:240',
        words[d-1],
        ])

Here are some examples of what the original file would look like:

 ffmpeg -i SR.mp4 -ss 00:00:00 -to 00:01:22 -c:v copy -a copy CPH.mp4
 ffmpeg -i SR.mp4 -ss 00:01:24 -to 00:02:58 -c:v copy -a copy CG.mp4
 ffmpeg -i SR.mp4 -ss 00:02:59 -to 00:05:41 -c:v copy -a copy CSGP.mp4

Nothing fancy just separating video in its own individual segments and then resaving it before resizing it.

I tried:

z=subprocess.call(f, shell=True, stdout=subprocess.PIPE)
print(z)

But all I get is '1'.

When I changed it to:

z=subprocess.call(f, shell=True, stderr=subprocess.PIPE)
print(z)

All I get is '1'.

Maybe I'm doing something wrong.

1

There are 1 best solutions below

2
Hai Vu On

Based on how I understand your problem, here is what I came up with. My system does not have ffmpeg installed, nor do I have any video to test out, so I comment out the subprocess.run() line. All do is to transform the lines from the text file SRt.txt into commands which you can invoke.

with open("SRt.txt") as stream:
    for line_number, line in enumerate(stream):
        # line_number=0 for line 1,
        # line_number=1 for line 2, ...

        command = line.split()

        # Last file name is tokens[-1]
        command[-1] = f"SR{line_number}.mp4"

        # Insert -vf scale=320:240
        command[-2:-1] = ["-vf", "scale=320:240"]

        print(line, end="")
        print(command)
        print()
        # subprocess.run(command, shell=True)

Content of my sample SRt.txt

ffmpeg -i SR.mp4 -ss 00:00:00 -to 00:01:22 -c:v copy -a copy CPH.mp4
ffmpeg -i foo.mp4 -ss 00:01:24 -to 00:02:58 -c:v copy -a copy CG.mp4
ffmpeg -i bar.mp4 -ss 00:02:59 -to 00:05:41 -c:v copy -a copy CSGP.mp4

Output:

ffmpeg -i SR.mp4 -ss 00:00:00 -to 00:01:22 -c:v copy -a copy CPH.mp4
['ffmpeg', '-i', 'SR.mp4', '-ss', '00:00:00', '-to', '00:01:22', '-c:v', 'copy', '-a', '-vf', 'scale=320:240', 'SR0.mp4']

ffmpeg -i foo.mp4 -ss 00:01:24 -to 00:02:58 -c:v copy -a copy CG.mp4
['ffmpeg', '-i', 'foo.mp4', '-ss', '00:01:24', '-to', '00:02:58', '-c:v', 'copy', '-a', '-vf', 'scale=320:240', 'SR1.mp4']

ffmpeg -i bar.mp4 -ss 00:02:59 -to 00:05:41 -c:v copy -a copy CSGP.mp4
['ffmpeg', '-i', 'bar.mp4', '-ss', '00:02:59', '-to', '00:05:41', '-c:v', 'copy', '-a', '-vf', 'scale=320:240', 'SR2.mp4']

Note: If you don't want the -vf scale=320:240, part, comment it out