-1
# optional cropping
if [ "$1" == "cropit" ]; then
   ITS_CROP_TIME=
   mkdir cropped; for i in *.mp4; do ffmpeg -i "$i" -filter:v "crop=1920:980:0:-100" cropped/"${i%.*}.mp4"
   rm -r *.mp4
   cd cropped
   cp -r *.mp4 ../
fi
# optional cropping

Am I allowed to use ITS_CROP_TIME= string in this way (to capture the output of the code below)? And if I now go place this variable in my script somewhere, will the commands be executed at the variable definition?

What about this? Will this work?

if [ "$1" == "cropit" ]; then
   ITS_CROP_TIME=mkdir cropped; for i in *.mp4; do ffmpeg -i "$i" -filter:v "crop=1920:980:0:-100" cropped/"${i%.*}.mp4"; rm -r *.mp4; cd cropped; cp -r *.mp4 ../
fi
  • 3
    The statement ITS_CROP_TIME= assigns to $ITS_CROP_TIME an empty string. It sounds vaguely like your goal, though, is to define a bash function. If so, read about bash functions. – John1024 May 25 at 2:36
  • It's not empty, look at all the code below it. Thats what I am asking. Will those codes belong to that string? – Anonymous May 25 at 2:44
  • 2
    What are you wanting to do with ITS_CROP_TIME ? I see you croppng videos, but what is ITS_CROP_TIME for? – Jasen May 25 at 3:41
0
#!/bin/bash
set -e
if [ "$1" == "cropit" ]; then
   ITS_CROP_TIME="$(
        mkdir cropped;
        for i in *.mp4; do 
           ffmpeg -i "$i" -filter:v "crop=1920:980:0:-100" cropped/"${i%.*}.mp4"
        done
        rm -r ./*.mp4
        cd cropped
        cp -r ./*.mp4 ../
    )"
fi

https://www.shellcheck.net/

  • 5
    This executes the commands immediately, and stores their output in ITS_CROP_TIME. My understanding of the question is that they want to store the commands in a variable for execution later, which is completely different. And not what variables are for at all; use a function instead of a variable. – Gordon Davisson May 25 at 6:38
0

the first is allowed, the second will not work, but if you put a semicolon before mkdir it will also work.

  • 2
    What do you mean by "work"? Run without errors, or do what the user wants to do? – Kusalananda May 25 at 7:04
  • 2
    Run without errors, I can't read minds, but given that ffmpeg needs great skill to use the code in at least one of the examples must be tested. – Jasen May 25 at 10:24
-1

I'm assuming that you'd want to set the variable ITS_CROP_TIME to a collection of commands for running later?

That's what functions are for:

crop_all_in_dir () {
    local source_dir="$1"
    local dest_dir="$2"

    mkdir -p "$dest_dir"

    for file in "$source_dir"/*.mp4; do
        [ ! -f "$file" ] && continue
        ffmpeg -i "$file" -filter:v "crop=1920:980:0:-100" "$dest_dir/(basename "${file%.*}").mp4"
    done
}

Then you may want to call this as

crop_all_in_dir . cropped

i.e., crop all the *.mp4 files in the current directory and place the cropped files in the cropped directory (a subdirectory of the current directory), creating the output directory if it does not already exist.

The function does the cropping non-destructively, so any replacing of the original files (your cp etc.) would be done from outside of that function.

  • That cool, and thank you... but I am trying to run this within a bigger script, not by itself. That would require extra steps. That's why the notes say # optional cropping – Anonymous May 26 at 4:08
  • I am too much of a noob still, sorry. I am trying really hard though. – Anonymous May 26 at 4:12
  • @Anonymous The function would be part of the bigger script. – Kusalananda May 26 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.