Search code examples
linuxbashffmpeg

ffmpeg doesn't accept input in script


this is a beginner's question but i can't figure out the answer after looking into it for several days:

I want ffmpeg to extract the audio portion of a video and save it in an .ogg container. If i run the following command in terminal it works as expected:

ffmpeg -i example.webm -vn -acodec copy example.ogg

For convenience, i want to do this in a script. However, if i pass a variable to ffmpeg it apparently just considers the first word and produces the error "No such file or directory".

I noticed that my terminal escapes spaces by a \ so i included this in my script. This doesn't solve the problem though.

Can someone please explain to me, why ffmpeg doesn't consider the whole variable that is passed to it in a script while working correctly when getting passed the same content in the terminal?

This is my script that passes the filename with spaces escaped by \ to ffmpeg:

#!/bin/bash

titelschr=$(echo $@ | sed "s/ /\\\ /g")
titelohne=$(echo $titelschr | cut -d. -f 1)
titelogg=$(echo -e ${titelohne}.ogg)  

ffmpeg -i $titelschr -vn -acodec copy $titelogg

Thank you very much in advance!


Solution

  • You need to quote variable expansions, try this :

    #!/usr/bin/env bash
    
    titelschr=$1
    titelogg="${titelschr%.*}.ogg"
    
    ffmpeg -i "$titelschr" -vn -acodec copy "$titelogg"
    

    call with :

    bash test.sh "Some video file.mp4"
    

    This way, you don't need to escape spaces.