Question

I have been scratching my head with this one for hours.

I'm trying to write a simple script to convert old videos in .AVI format to .mp4 with HandbrakeCLI and I can not get Handbrake to register the correct arguments, I keep getting the "Missing output file name. Run C:/Program Files/Handbrake/HandbrakeCLI.exe --help for syntax.\r\n" error.

Heres what I have so far

import glob
import os

import handbrake

hb = handbrake.HandbrakeEncode
mydir = "C:\\Path\\To\\MyVids\\"
os.chdir(mydir)

filesList = []

for files in glob.glob("*.avi"):
    filesList.append(mydir + files)
    print(mydir + files) 
    #this prints the correctly assembled path and file as expected

for files in filesList:
    print("Encoding file: " + files)
    hb(files)

and the hb function is:

def HandbrakeEncode(filepath):
    import subprocess
    import os
    from subprocess import Popen, PIPE
    outputPath, fileExtension = os.path.splitext(filepath)
    outputPath += ".mp4"

    args = '-i ' + filepath + ' -o '+ outputPath


    cmd = ['C:\\Program Files\\Handbrake\\HandbrakeCLI.exe', args]


    p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdout, stderr = p.communicate()
    print(stdout)

Thanks for any help you can offer...

Was it helpful?

Solution

subprocess.Popen expects that the command is split up into escapable chunks:

['foo', '-a', 'bar', '--baz']

Your command should be a list of arguments:

cmd = [
    'C:\\Program Files\\Handbrake\\HandbrakeCLI.exe',
    '-i', filepath,
    '-o', outputPath
]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top