You are not logged in.

#1 2013-12-09 04:46:55

z1lt0id
Member
Registered: 2012-09-20
Posts: 177

[SOLVED] Bash Scripting - Question regarding if statement.

I have a bash script where it asks you how many video files you want to concat together.  It asks you how many video files there are and then does the following

if [ "$segments_variable" = "2" ]; then
        ffmpeg -i "concat:01.h264|02.h264" -c copy video.h264
fi
if [ "$segments_variable" = "3" ]; then
        ffmpeg -i "concat:01.h264|02.h264|03.h264" -c copy video.h264
fi
if [ "$segments_variable" = "4" ]; then
        ffmpeg -i "concat:01.h264|02.h264|03.h264|04.h264" -c copy video.h264
fi

How would I make it so you could put in say 40 and it would be able to do it without make 40 if statements?

Last edited by z1lt0id (2013-12-09 22:28:58)

Offline

#2 2013-12-09 04:53:13

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,740

Re: [SOLVED] Bash Scripting - Question regarding if statement.

A for loop comes to immediately to mind.  Second Duck-Duck-Go hit on "Bash for loop" : http://www.thegeekstuff.com/2011/07/bas … -examples/


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2013-12-09 04:58:14

z1lt0id
Member
Registered: 2012-09-20
Posts: 177

Re: [SOLVED] Bash Scripting - Question regarding if statement.

I understand how you would use a loop function, but  I guess where I am more stumped is with how to add to the ffmpeg line where I need to add

concat:01.h264|02.h264|03.h264| etc etc

That is what has me stumped.

Offline

#4 2013-12-09 05:03:11

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,466

Re: [SOLVED] Bash Scripting - Question regarding if statement.

This is perfect for a case statement.

nevermind, didn't read. Are the files always named with numbers like that or are you just using that to demonstrate?

Last edited by Scimmia (2013-12-09 05:06:29)

Offline

#5 2013-12-09 05:05:46

z1lt0id
Member
Registered: 2012-09-20
Posts: 177

Re: [SOLVED] Bash Scripting - Question regarding if statement.

But wouldn't a case statment be like

2)
 ffmpeg -i "concat:01.h264|02.h264" -c copy video.h264;
3)
 ffmpeg -i "concat:01.h264|02.h264|03.h264" -c copy video.h264;

Offline

#6 2013-12-09 05:05:47

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,740

Re: [SOLVED] Bash Scripting - Question regarding if statement.

I see what you are asking.  Build the 01.h264|02.h264|03.h264|04.h264..... string inside the for loop and call it something, say $CATSTRING.  After you fall outside the loop, then do something like:
ffmpeg -i $CATSTRING -c copy video.h264


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#7 2013-12-09 05:34:16

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,466

Re: [SOLVED] Bash Scripting - Question regarding if statement.

If those are the actual names, you don't even need a loop, just bash brace expansion.

filelist=$(echo {01..$1}.h264)
ffmpeg -i "concat:${filelist// /|}" -c copy video.h264

Offline

#8 2013-12-09 08:24:07

z1lt0id
Member
Registered: 2012-09-20
Posts: 177

Re: [SOLVED] Bash Scripting - Question regarding if statement.

Interesting enough.  When I use a variable in the first line it will come out for example as {01..06}.h264 instead 01.h264 02.h264 03.h264, etc.

Offline

#9 2013-12-09 10:08:14

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Bash Scripting - Question regarding if statement.

Scimmia wrote:
filelist=$(echo {01..$1}.h264)

This doesn't work because brace expansion is done before parameter expansion. One way around this is using eval, of course.

@z1lt0id

I don't know your convention for leading zeros (always pad to length 2? how about 100.h264 then?); just an example:

#!/bin/bash

segments_variable=10

segments_list=
for ((i=1; i<=segments_variable; i++)); do
  segments_list+=$(printf '%0*d.h264|' ${#segments_variable} "$i")
done
segments_list=${segments_list%\|}

ffmpeg -i "concat:$segments_list" -c copy video.h264

This silver ladybug at line 28...

Offline

#10 2013-12-09 10:28:27

z1lt0id
Member
Registered: 2012-09-20
Posts: 177

Re: [SOLVED] Bash Scripting - Question regarding if statement.

I realised padding makes no sense in that regard.  But it would never get to 100 segments.  But thanks for the below example I'll try it out and adapt it.

Offline

#11 2013-12-09 10:51:07

satanselbow
Member
Registered: 2011-06-15
Posts: 538

Re: [SOLVED] Bash Scripting - Question regarding if statement.

Would it not be more sane (portable) to feed your script the video files from the command line?

You could then make use of bash's "$@" as an array to feed your looped concat..

fileArray=("$@")

for arg in "${fileArray[@]}"; do
   echo "$arg" // do your concat here as per lolilolicon/Scimmia
done

There are numerous bash tutorials knocking about to do this sort of iteration... google is your friend...

Offline

#12 2013-12-09 11:42:43

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Bash Scripting - Question regarding if statement.

z1lt0id wrote:

I realised padding makes no sense in that regard.

In the absence of zero padding, an IFS trick would be more convenient:

#!/bin/bash

segments_variable=10

eval segments=\({1..$segments_variable}.h264\)

ifs=$IFS
IFS='|'
ffmpeg -i "concat:${segments[*]}" -c copy video.h264
IFS=$ifs

This silver ladybug at line 28...

Offline

#13 2013-12-09 16:59:16

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,466

Re: [SOLVED] Bash Scripting - Question regarding if statement.

lolilolicon wrote:
Scimmia wrote:
filelist=$(echo {01..$1}.h264)

This doesn't work because brace expansion is done before parameter expansion. One way around this is using eval, of course.

Ah, I was testing in zsh, where it works perfectly. Forgot to try it in bash.

Offline

#14 2013-12-09 22:28:39

z1lt0id
Member
Registered: 2012-09-20
Posts: 177

Re: [SOLVED] Bash Scripting - Question regarding if statement.

lolilolicon wrote:
Scimmia wrote:
filelist=$(echo {01..$1}.h264)

This doesn't work because brace expansion is done before parameter expansion. One way around this is using eval, of course.

@z1lt0id

I don't know your convention for leading zeros (always pad to length 2? how about 100.h264 then?); just an example:

#!/bin/bash

segments_variable=10

segments_list=
for ((i=1; i<=segments_variable; i++)); do
  segments_list+=$(printf '%0*d.h264|' ${#segments_variable} "$i")
done
segments_list=${segments_list%\|}

ffmpeg -i "concat:$segments_list" -c copy video.h264

This actually works perfectly smile  Thank you to everyone that helped me out.

Offline

Board footer

Powered by FluxBB