You are not logged in.
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
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
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
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
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
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
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
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
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
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
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
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
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
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 Thank you to everyone that helped me out.
Offline