Search code examples
pythonffmpegsubprocesspathlib

Spaces in FFMPEG Input Thwart Execution


What's so baffling, is why spaces in the audio input seem to work fine, but spaces in the video input break things. This fetches random images from a directory, pairs them with random audio clips and joins them into an mp4 video.

def makeAudioImgMovs():
    randImgs = getImgs()
    compAudClips = makeAudioClips()
    audioJpgs = dict(zip(randImgs,compAudClips)) #Heres where we join the completed audio clips and the jpgs into a dict
    for jpg, aud in audioJpgs.items():
        print(f'The img is {jpg}, the type of the img is {type(jpg)} the aud is {aud} the type of the aud is {type(aud)}')
        subprocess.call('ffmpeg -hide_banner -loglevel error -loop 1 -i "{0}" -i "{1}"\
        -filter:v \
        "fade=in:st=0:d=1, fade=out:st=10:d=1,scale=720:480:force_original_aspect_ratio=decrease,pad=720:480:(ow-iw)/2:(oh-ih)/2,setsar=1" \
         -filter:a "afade=in:st=0:d=1, afade=out:st=10:d=1" \
        -c:v libx264 -t 11 -pix_fmt yuv420p -preset faster -shortest -c:a aac {2}.mp4'.format(jpg,aud,timePath/(pathlib.Path(jpg).stem)),shell=True)
makeAudioImgMovs()

The print statement is writing what is in the dict of audioJpgs list. For example...

The img is 2021-09-02-1-40/ponty_me.jpg, the type of the img is <class 'pathlib.PosixPath'> the aud is 2021-09-02-1-40/Jms Brn like it is like it was.aiff the type of the aud is <class 'pathlib.PosixPath'>

The img is 2021-09-02-1-40/1966taxiToMassFromMomBack 2.tiff, the type of the img is <class 'pathlib.PosixPath'> the aud is 2021-09-02-1-40/Lola_Fay.mp3 the type of the aud is <class 'pathlib.PosixPath'>

[NULL @ 0x7fe503021400] Unable to find a suitable output format for '2021-09-02-1-40/1966taxiToMassFromMomBack' 2021-09-02-1-40/1966taxiToMassFromMomBack: Invalid argument

The img is 2021-09-02-1-40/1966taxiToMassFromMomBack 5.tiff, the type of the img is <class 'pathlib.PosixPath'> the aud is 2021-09-02-1-40/M Miller Lonnie's Lament.aiff the type of the aud is <class 'pathlib.PosixPath'>

[NULL @ 0x7fd32a854c00] Unable to find a suitable output format for '2021-09-02-1-40/1966taxiToMassFromMomBack' 2021-09-02-1-40/1966taxiToMassFromMomBack: Invalid argument

The img is 2021-09-02-1-40/10_021390_danny? 1_jpg.jpg, the type of the img is <class 'pathlib.PosixPath'> the aud is 2021-09-02-1-40/i_got_to_rock.mp3 the type of the aud is <class 'pathlib.PosixPath'>

[NULL @ 0x7f8449835800] Unable to find a suitable output format for '2021-09-02-1-40/10_021390_danny?' 2021-09-02-1-40/10_021390_danny?: Invalid argument

The img is 2021-09-02-1-40/11_021390_danny_2_jpg.jpg, the type of the img is <class 'pathlib.PosixPath'> the aud is 2021-09-02-1-40/M Miller Power.aiff the type of the aud is <class 'pathlib.PosixPath'>

The last pair of img/aud complete fine and write an mp4. Notice there, that the audio file has spaces in it! Notice that the errors 'Invalid argument' get thrown where there is a space in the jpg file name.

Help greatly appreciated.


Solution

  • You need to use double quotes here:

    ffmpeg ... -c:a aac "{2}.mp4"