Bosse_B
Posts: 989
Joined: Thu Jan 30, 2014 9:53 am

Command not working from script but on command line - why?

Mon Jun 15, 2020 8:03 pm

System:
RPi4 running Raspbian Buster fully updated.
ffmpeg is version: 4.1.4-1+rpt7~deb10u1

I have created a script to automate a video cropping function executed via ffmpeg.
In principle the script looks like this with error catching removed:

Code: Select all

#!/bin/bash
CROP="$1"
SOURCEFILE="$2"
TARGETFILE="$3"

COMMAND="ffmpeg -i $SOURCEFILE -filter:v \"crop=in_w:in_h-$CROP\" -c:a copy -hide_banner $TARGETFILE"
echo "Command to execute: $COMMAND"
$COMMAND
The script file is set to executable and placed in /home/pi/bin
In another dir /home/pi/video I have a test video file.
So from the video dir I issue the following command and get the error:

Code: Select all

 $ cropvideoh 40 videotest.mp4 crop40.mp4
Command to execute: ffmpeg -i videotest.mp4 -filter:v "crop=in_w:in_h-40" -c:a copy -hide_banner crop40.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'videotest.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:02:00.00, start: 0.000000, bitrate: 237 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, bt709), 320x240 [SAR 1:1 DAR 4:3], 135 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 06/15/2020.
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96 kb/s (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 06/15/2020.
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[AVFilterGraph @ 0x7bb460] No such filter: '"crop'   <==== ERROR HERE!!!!!
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!
So I tested again using the command that was printed to the console as part of script operation:

Code: Select all

$ ffmpeg -i videotest.mp4 -filter:v "crop=in_w:in_h-40" -c:a copy -hide_banner crop40.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'videotest.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:02:00.00, start: 0.000000, bitrate: 237 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, bt709), 320x240 [SAR 1:1 DAR 4:3], 135 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 06/15/2020.
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96 kb/s (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 06/15/2020.
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libx264 @ 0x21c7e70] using SAR=1/1
[libx264 @ 0x21c7e70] using cpu capabilities: ARMv6 NEON
[libx264 @ 0x21c7e70] profile High, level 1.3
[libx264 @ 0x21c7e70] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'crop40.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 320x200 [SAR 1:1 DAR 8:5], q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 06/15/2020.
      encoder         : Lavc58.35.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96 kb/s (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 06/15/2020.
frame= 3000 fps=281 q=-1.0 Lsize=    2529kB time=00:01:59.97 bitrate= 172.7kbits/s speed=11.2x
video:1025kB audio:1407kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.983594%
[libx264 @ 0x21c7e70] frame I:16    Avg QP:18.72  size: 14149
[libx264 @ 0x21c7e70] frame P:1222  Avg QP:22.55  size:   594
[libx264 @ 0x21c7e70] frame B:1762  Avg QP:31.51  size:    55
[libx264 @ 0x21c7e70] consecutive B-frames: 14.7% 15.8% 15.2% 54.3%
[libx264 @ 0x21c7e70] mb I  I16..4: 11.6% 13.5% 74.9%
[libx264 @ 0x21c7e70] mb P  I16..4:  0.7%  0.3%  0.2%  P16..4: 19.0%  6.6%  4.5%  0.0%  0.0%    skip:68.6%
[libx264 @ 0x21c7e70] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8: 11.2%  0.7%  0.1%  direct: 0.2%  skip:87.8%  L0:36.7% L1:60.3% BI: 3.0%
[libx264 @ 0x21c7e70] 8x8 transform intra:19.4% inter:43.7%
[libx264 @ 0x21c7e70] coded y,uvDC,uvAC intra: 56.8% 77.6% 59.5% inter: 4.0% 5.6% 1.6%
[libx264 @ 0x21c7e70] i16 v,h,dc,p: 15% 75%  5%  5%
[libx264 @ 0x21c7e70] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 18% 33% 24%  3%  3%  4%  5%  4%  5%
[libx264 @ 0x21c7e70] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 34% 12%  4%  5%  5%  6%  5%  6%
[libx264 @ 0x21c7e70] i8c dc,h,v,p: 26% 51% 18%  5%
[libx264 @ 0x21c7e70] Weighted P-Frames: Y:0.3% UV:0.1%
[libx264 @ 0x21c7e70] ref P L0: 80.8%  7.8%  8.9%  2.5%  0.0%
[libx264 @ 0x21c7e70] ref B L0: 91.3%  7.6%  1.1%
[libx264 @ 0x21c7e70] ref B L1: 96.6%  3.4%
[libx264 @ 0x21c7e70] kb/s:69.95
And when it all ends I have the cropped video!

QUESTION:
Why is this not working when I run the exact same command from within the script????
And what can I do to fix it?
Bo Berglund
Sweden

W. H. Heydt
Posts: 12682
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Command not working from script but on command line - why?

Mon Jun 15, 2020 8:09 pm

I don't know about this particular case, but problems like that often come down to making sure that the environmental variables, especially PATH are set properly. Also...are you sure you need quotes around the command line arguments? As a test, I would suggest echoing your variables to see if they have the values you assume they do.

RonR
Posts: 1201
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Command not working from script but on command line - why?

Mon Jun 15, 2020 9:07 pm

Bosse_B wrote:
Mon Jun 15, 2020 8:03 pm

Code: Select all

echo "Command to execute: $COMMAND"
$COMMAND

Try instead:

Code: Select all

echo "Command to execute: $COMMAND"
eval $COMMAND

Bosse_B
Posts: 989
Joined: Thu Jan 30, 2014 9:53 am

Re: Command not working from script but on command line - why?

Mon Jun 15, 2020 9:15 pm

W. H. Heydt wrote:
Mon Jun 15, 2020 8:09 pm
I don't know about this particular case, but problems like that often come down to making sure that the environmental variables, especially PATH are set properly. Also...are you sure you need quotes around the command line arguments? As a test, I would suggest echoing your variables to see if they have the values you assume they do.
Regarding quotes I assume you mean these:

Code: Select all

CROP="$1"
SOURCEFILE="$2"
TARGETFILE="$3"
I have always done this before without any errors or malfunction.
The problem with the script is from ffmpeg as follows:

Code: Select all

Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[AVFilterGraph @ 0x7bb460] No such filter: '"crop'   <==== ERROR HERE!!!!!
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
The same place in the working command line version is:

Code: Select all

Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libx264 @ 0x21c7e70] using SAR=1/1
Note that the No such filter: "'crop' comes from the ffmpeg command line:

Code: Select all

ffmpeg -i videotest.mp4 -filter:v "crop=in_w:in_h-40" -c:a copy -hide_banner crop40.mp4
where the filter is the ffmpeg subcommand crop
There must be a problem in the way the script sends the command to the system.
Note that for debug I echo the command to the screen so I can have a look at what was produced.
When it failed I copied the echoed command and executed it directly and it worked!

This is the bugger, why does a working command NOT WORK when used from within a script????
Bo Berglund
Sweden

Bosse_B
Posts: 989
Joined: Thu Jan 30, 2014 9:53 am

Re: Command not working from script but on command line - why?

Mon Jun 15, 2020 9:19 pm

RonR wrote:
Mon Jun 15, 2020 9:07 pm
Try instead:

Code: Select all

echo "Command to execute: $COMMAND"
eval $COMMAND
THANKS A MILLION!
That did work!

What is happening when using eval rather than just the command?
Bo Berglund
Sweden

RonR
Posts: 1201
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Command not working from script but on command line - why?

Mon Jun 15, 2020 9:35 pm

Bosse_B wrote:
Mon Jun 15, 2020 9:19 pm
What is happening when using eval rather than just the command?

A Google search will get you exhaustive explanations and examples. One is:

eval is a built in linux or unix command. The eval command is used to execute the arguments as a shell command on unix or linux system. Eval command comes in handy when you have a unix or linux command stored in a variable and you want to execute that command stored in the string. The eval command first evaluates the argument and then runs the command stored in the argument.

Return to “General discussion”