You are not logged in.

#1 2020-01-21 09:35:11

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,070
Website

[SOLVED] Array expansion to command?

This test

#!/bin/bash

encode() {

    input="$HOME/tmp/t e s t.mp4"
    output="$HOME/tmp/out.mp4"

    encarr=("ffmpeg")
    encarr+=("-i ${input}")
    encarr+=("-vf yadif=0") # deinterlace
    encarr+=("-pix_fmt yuv420p")
    encarr+=("-c:v libx264")
    encarr+=("-preset slow")
    encarr+=("-tune film")
    encarr+=("-crf 18")
    encarr+=("-threads 0")
    encarr+=("-an")
    #encarr+=("-aspect 16:9")
    #encarr+=("-level 4.1")
    encarr+=("${output}")
    encarr+=("-movflags +faststart -loglevel panic -stats")

    # preview
    for i in "${!encarr[@]}"; do 
        printf "%s\t%s\n" "$i" "${encarr[$i]}"
    done
        
    # action
    "${encarr[@]}"

}
encode

will fail with "Unrecognized option 'i /mnt/...'.", if I unquote  "${encarr[@]}" it will fail due to spaces in input, what is the way to do this correctly?

Last edited by brontosaurusrex (2020-01-21 14:59:48)

Offline

#2 2020-01-21 11:08:13

clusterF
Member
Registered: 2019-05-07
Posts: 539

Re: [SOLVED] Array expansion to command?

This just from a novice, but wouldn't "input="$HOME/tmp/t e s t.mp4" be the issue? Spaces in file names would be treated as directories?

Offline

#3 2020-01-21 11:49:22

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,070
Website

Re: [SOLVED] Array expansion to command?

Well, dunno, this works for example

#!/bin/bash

encode() {

    input="$HOME/tmp/t e s t.mp4"
    output="$HOME/tmp/out.mp4"

    encarr=(ffmpeg)
    encarr+=(-i "$input") # fixed
    encarr+=(-vf yadif=0)
    encarr+=(-pix_fmt yuv420p)
    encarr+=(-c:v libx264)
    encarr+=(-preset fast)
    encarr+=(-tune film)
    encarr+=(-crf 18)
    encarr+=(-threads 0)
    encarr+=(-an)
    #encarr+=(-aspect 16:9)
    #encarr+=(-level 4.1)
    encarr+=("$output")
    encarr+=(-movflags +faststart -loglevel panic -stats)

    # preview
    for i in "${!encarr[@]}"; do 
        printf "%s\t%s\n" "$i" "${encarr[$i]}"
    done
        
    # action
    "${encarr[@]}"

}
encode

but it's not human-pretty-readable, returns:

0	ffmpeg
1	-i
2	/home/ticho/tmp/t e s t.mp4
3	-vf
4	yadif=0
5	-pix_fmt
6	yuv420p
7	-c:v
8	libx264
9	-preset
10	fast
11	-tune
12	film
13	-crf
14	18
15	-threads
16	0
17	-an
18	/home/ticho/tmp/out.mp4
19	-movflags
20	+faststart
21	-loglevel
22	panic
23	-stats

Perhaps I'll just use this and blame ffmpeg for ugliness.

Last edited by brontosaurusrex (2020-01-22 10:52:29)

Offline

#4 2020-01-21 12:23:18

rbh
Member
From: Sweden/Vasterbotten/Rusfors
Registered: 2016-08-11
Posts: 562

Re: [SOLVED] Array expansion to command?

Not when qouted ("  ").

Cd $HOME/tmp/t e s t.mp4"

, brings me to directory ~/tmp/t e s t.mp4...


edit:

dropped one quote, should been: Cd "$HOME/tmp/t e s t.mp4"

Last edited by rbh (2020-01-21 16:20:53)


// Regards rbh

Online

#5 2020-01-21 12:37:44

clusterF
Member
Registered: 2019-05-07
Posts: 539

Re: [SOLVED] Array expansion to command?

rbh wrote:
clusterF wrote:

This just from a novice, but wouldn't "input="$HOME/tmp/t e s t.mp4" be the issue? Spaces in file names would be treated as directories?

Not when qouted ("  ").

Cd $HOME/tmp/t e s t.mp4"

, brings me to directory ~/tmp/t e s t.mp4...

Never knew that. Thanks

Not sure i get the gist of the script though, looks like a media converter maybe?

Offline

#6 2020-01-21 12:58:48

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,070
Website

Re: [SOLVED] Array expansion to command?

Right, part of it, other parts are audio encoder, r128 thing, some logic around hasAudio, ..., some logic around audio channel types and similar.

Offline

#7 2020-01-21 13:48:16

damo
....moderator....
Registered: 2015-08-20
Posts: 6,124

Re: [SOLVED] Array expansion to command?

What happens if you have

encarr+=("-i \"${input}\"")

Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#8 2020-01-21 14:22:37

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,070
Website

Re: [SOLVED] Array expansion to command?

@damo:

Unrecognized option 'i "/home/ticho/tmp/t e s t.mp4"'.

I guess ffmpeg want's this to get as two separated parameters (which actually makes sense).

Offline

#9 2020-01-22 03:09:15

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,146
Website

Re: [SOLVED] Array expansion to command?

^Right, all those array elements need to be separate words, unless you want to pass something with spaces as one word.
ie

encarr+=(-vf)
encarr+=(yadif=0)

But [SOLVED] means you've got that already. smile


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#10 2020-01-22 09:07:21

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,070
Website

Re: [SOLVED] Array expansion to command?

That, so what can I/we learned from this, this are two elements of array

encarr+=(-i)
encarr+=("$input")

(and I don't know how to write that as single assignment).

And btw this are also two elements of an array

encarr+=(-vf yadif=0)

Offline

#11 2020-01-22 09:52:54

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,146
Website

Re: [SOLVED] Array expansion to command?

^yes

And "single assignment": does this not work?

encarr+=(-i "$input")

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#12 2020-01-22 10:51:11

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,070
Website

Re: [SOLVED] Array expansion to command?

Duh, of course it does, so I should I mark this "[SOLVED] and [SOLVED]"?

edit, proof:

file="/some/f i l e"

arr1=(-i "$file")

arr2=(-i)
arr2+=("$file")

[[ ${arr1[@]} == ${arr2[@]} ]] && echo "ok" 
ok

Last edited by brontosaurusrex (2020-01-23 13:40:07)

Offline

#13 2020-01-23 00:22:04

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,146
Website

Re: [SOLVED] Array expansion to command?

^That test doesn't check how the words are divided up though, does it?
Wouldn't

two words "second pair"

evaluate the same as

"two words" second pair

??


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#14 2020-01-23 09:28:38

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,070
Website

Re: [SOLVED] Array expansion to command?

True^, that test is useless.

Offline

Board footer

Powered by FluxBB