Search code examples
pythonffmpegconcatenationmp4

Last subprocess call not working in concatenation code with FFMPEG. How should I go about fixing this?


clips = []

#generates a list of mp4 files in a folder
def clipFinder(CurrentDir, fileType):
    clips.clear()
    for r,d,f in os.walk(CurrentDir):
        for file in f:
            if fileType in file:
                clips.append(r+file)
    random.shuffle(clips)

#removes all files that have the string 'vod' in them as they cause problems during concatenation
def removeVods(r):
    for f in clips:
        if 'vod' in clips:
            os.remove(r+f)

#generates a string using the clips list to insert into the ffmpeg command
def clipString():
    string = 'intermediate'
    clipList = []
    clipNum = 1
    for f in clips:
        clipList.append(string+str(clipNum)+'.ts'+'|')
        clipNum+=1
    string1 = ''.join(clipList)
    string2 = string1[0:len(string1)-1]
    return string2

#concatenates the mp4 files in the clipString
def concatFiles():
    clipFinder('***', '.mp4')
    removeVods('***')
    i = 0
    intermediates = []
    for f in clips:
        subprocess.call(['***', '-i', clips[i], '-c', 'copy', '-bsf:v', 'h264_mp4toannexb', '-f', 'mpegts', 'intermediate'+ str(i+1) +'.ts'])
        i += 1 
    clipsLength = len(clips)
    subprocess.call['***', '-i', '"concat:' + clipString() + '"', '-c', 'copy', '-bsf:a 
    aac_adtstoasc', 'output.mp4']

I am trying to make a clip concatenator, but the last subprocess call won't run and gives me no error. When I run the script the first FFmpeg call works fine and gives me my intermediate mp4 files, however, the second command, which works when I run it in terminal, does not work when I run it from python using subprocess.call.

Problematic code:

subprocess.call(['***', '-i', '"concat:' + clipString() + '"', '-c', 'copy', '-bsf:a aac_adtstoasc', 'output.mp4'])

all places with * were paths such as: /davidscomputer/bin/ffmpeg/


Solution

  • The problem code: subprocess.call['***', '-i', '"concat:' + clipString() + '"', '-c', 'copy', '-bsf:a aac_adtstoasc', 'output.mp4']

    Solution: commandString = ['/Users/teoscomputer/bin/ffmpeg', '-i', 'concat:' + clipString(), '-c', 'copy', '-bsf:a', 'aac_adtstoasc', 'output.mp4'] subprocess.Popen(commandString)

    There were two problems: 1) the, '-bsf:a aac_adtstoasc' needed to be separated into, '-bsf:a', 'aac_adtstoasc'

    2)the " quotes around the concat needed to be removed as they are only needed when running a direct command in the shell