Edit: Thank you all for your quick responses! No downtime at all with this project! :D More specifically, I followed the advice to put command line parameters in the script. By slapping a $1 anywhere I want the filename to be expanded/accepted in the script, I was able to do

./command_ffmpeg_SYNCAUD  birthday_0  

I am digitizing my dad’s video and audio tapes, for which I - after painstakingly scouring the documentation of ffmpeg, as one should - finally have found the encoding options that I’m happy with. :D

But instead of retyping the PhD dissertation that are the ffmpeg commands that I use a bazillion times, I have simply started to do command substitution with for instance

$(cat command_ffmpeg_COPYCODEC)  

or

$(cat command_ffmpeg_H264)  

and the likes. This, however, still requires me to vim the correct filenames into these concatenated commands every time I work on a new project, so I thought, "Why not just set a variable, like I use to do with visudo as in

EDITOR=/bin/vim visudo  

So, wanting to do a little ffmpeg magic to delay the audio stream of a file, I tried

FILENAME='birthday_0' $(cat command_ffmpeg_SYNCAUD)  

but bash said, “no, who do you think you are?” After doing a wc on FILENAME, I noticed that there is an invisible trailing something after the filename - possibly some whitespace char - since it thinks that I mean to use an option “-synced.mkv”.

KiHVde8Vymkdsyn.jpg

How would you go about writing the now bash script (it simply wouldn’t expand the variable through command substitution…) so that the trailing whatever is deleted, if that is indeed the case? I have tried slapping " ", ’ ', ( ), { } in all conceivable configurations, but I’m simply to inexperienced. xD

Please advise! 🛠️

  • hexagonwin@lemmy.today
    link
    fedilink
    English
    arrow-up
    3
    ·
    16 hours ago

    i’m also a noob, but why not just do something like

    #!/bin/sh
    ffmpeg (options) $1
    

    and run the script as ./script.sh filename.mkv?

  • thingsiplay@lemmy.ml
    link
    fedilink
    arrow-up
    7
    arrow-down
    1
    ·
    24 hours ago

    First, did you look if the filename itself contains a trailing whitespace? I had this issue a few times myself, thinking the script is broken, but in fact the file on the filesystem had a space. That is annoying. If your variable has an additional space and you want to remove it, then I think its better to find out where this space comes from (if its not the file itself), and solve the issue there. Because if you get an unexpected space, then this could be a bug in your script somewhere.

    There are multiple ways to remove leading and trailing whitespaces with Bash. The best is not to call a command for this and rely on Bash substitution. This can get ugly, but luckily we can steal from Stack Overflow, :p (basically what we did before Ai):

    trim() {
        local var="$*"
        # remove leading whitespace characters
        var="${var#"${var%%[![:space:]]*}"}"
        # remove trailing whitespace characters
        var="${var%"${var##*[![:space:]]}"}"
        printf '%s' "$var"
    }
    
    trim "    text in the middle          "
    

    https://stackoverflow.com/questions/369758/how-can-i-trim-whitespace-from-a-bash-variable (who got it from another source,

    • Consti@lemmy.world
      link
      fedilink
      arrow-up
      7
      ·
      24 hours ago

      If the filename has a space, a good solution without adjusting the files is to use quotes: "$FILENAME-synced.mkv"

      Edit: generally, as a good practice, quote all variables. If you had FILENAME="my file"and tried to use it asffprobe $FILENAMEit wouldn't work, because variables are evaluated *before* interpreting the command, so effectively it evaluates toffprobe my file(two arguments).ffprobe "$FILENAME"on the other hand evaluates toffprobe “my file”` (onr argument) and works properly no matter the file name.

      • thingsiplay@lemmy.ml
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        24 hours ago

        Or better rename the file. A trailing whitespace in a filename is most likely an error and not what you want to have. As this is about trailing whitespace, not just somewhere in the middle. In general I assume the user already tried and uses quotes.

        • Consti@lemmy.world
          link
          fedilink
          arrow-up
          4
          arrow-down
          1
          ·
          24 hours ago

          For trailing whitespace agreed, but a whitespace in the middle of the file is not uncommon and should be properly handled.

          • thingsiplay@lemmy.ml
            link
            fedilink
            arrow-up
            3
            arrow-down
            1
            ·
            24 hours ago

            I know that and didn’t say anything against it. Just saying the question is about trailing whitespaces and my suggestion was to look if the file contains trailing whitespace or if its a script error adding a whitespace to the variable. We don’t know so much about the issue.

  • talkingpumpkin@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    24 hours ago
    FILENAME='birthday_0' $(cat command_ffmpeg_SYNCAUD)  
    

    cat prints out a file without altering it’s contents.

    If it’s important to keep the commands in a separate file, you could use sed to find/replace, or use eval, or use some external template engine (lookup “bash expand variables in file”).

    What you probably want to do instead, I assume, is to replace the external files with regular bash variables:

    filename="some-file"
    command="ffmpg -i ${filename}.mpg -bla -blabla ${filename}.mkv"
    echo "$command"
    
  • Consti@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    1 day ago

    Why are you not simply writing a script with an argument? Why this cat indirection with environment variables?