You are not logged in.

#1 2011-03-13 19:39:38

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Bash script: Loop command,

Hi all,

I need a bit of help looping a command on a bash script,

please see below the command I need to loop:

ffmpeg -s 2720x1024 -r 25 -f x11grab -i :0.0 -acodec libmp3lame -ab 128k -vcodec mpeg4 -sameq -y -t 60 /media/z/.cam/screen/screen_`date +%F_%T`.avi

So basically I record my screen for a period of 1 minutes and the process once done need to restart.

I have search on how to loop the command but I must admit it seem rather complicated, so for the time being I added && at the end and paste the command a lot...

Any help would be appreciated,

Many thanks,

Regards

Offline

#2 2011-03-13 20:00:42

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash script: Loop command,

Do you have an ending condition for the loop? Or do you just want to do this X number of times? The section on 'Compound Commands' in bash's man page explains the syntax of the 3 looping constructs in bash: while, until, for.

Offline

#3 2011-03-13 20:03:14

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Bash script: Loop command,

No I do not have a ending conditions, basically I want it to run until the computer is switch off or the user is log out.

And I looked at it, and did some try but after few drinks I must be .....

Thanks for your help,

Offline

#4 2011-03-13 20:05:45

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash script: Loop command,

To make an infinite loop...

while true; do
  COMMANDS
done

Offline

#5 2011-03-13 20:08:18

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Bash script: Loop command,

Right, I have read that but I cannot make it work with my command for some reason,

would you be kind enought to give me an example with my command?

Many thanks

Offline

#6 2011-03-13 20:14:21

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash script: Loop command,

that _is_ an example. replace COMMANDS with ffmpeg.

Offline

#7 2011-03-13 23:42:55

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: Bash script: Loop command,

sweetthdevil wrote:

would you be kind enought to give me an example with my command?

while true; do
  ffmpeg -s 2720x1024 -r 25 -f x11grab -i :0.0 -acodec libmp3lame -ab 128k -vcodec mpeg4 -sameq -y -t 60 /media/z/.cam/screen/screen_`date +%F_%T`.avi
done

If that isn't working, you need to give us more information about exactly what you are experiencing, error messages etc.

Offline

#8 2011-03-13 23:58:39

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Bash script: Loop command,

Well I tried it but; it doesn't stop and create a new file every minute like the command demand.

It create a continues file instead.

P.s no error message.

Offline

#9 2011-03-14 08:05:55

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: Bash script: Loop command,

Does the ffmpeg command by itself stop after 60 seconds? Try adding some debugging like an echo command before/after the ffmpeg inside the loop.

Offline

#10 2011-03-14 08:57:07

3])
Member
From: Netherlands
Registered: 2009-10-12
Posts: 215

Re: Bash script: Loop command,

Just altered Fukawi's code.
Added a wait command

while true; do
  ffmpeg -s 2720x1024 -r 25 -f x11grab -i :0.0 -acodec libmp3lame -ab 128k -vcodec mpeg4 -sameq -y -t 60 /media/z/.cam/screen/screen_`date +%F_%T`.avi
  wait
done

Just add wait to it, will process the grab for a minute and then loop once more.
Only after the command has run completely that is.

Could be unnecessary but alas, I tried.

Last edited by 3]) (2011-03-14 09:00:28)


“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”-- C.A.R. Hoare

Offline

#11 2011-03-14 10:13:23

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash script: Loop command,

It's unnecessary. From TFM:

Waits for the process identified by ID, which may be a process ID or a
job specification, and reports its termination status.  If ID is not
given, waits for all currently active child processes, and the return
status is zero.  If ID is a a job specification, waits for all processes
in the job's pipeline.

The key part being 'child processes'. There are no child processes. Execution blocks while ffmpeg runs, and then the loop continues. If this loop isn't doing what the OP requires, then I suspect the issue is with the invocation of ffmpeg.

Offline

#12 2011-03-14 12:55:25

DIDI2002
Member
Registered: 2009-08-06
Posts: 66

Re: Bash script: Loop command,

while true;do ffmpeg -s 2720x1024 -r 25 -f x11grab -i :0.0 -acodec libmp3lame -ab 128k -vcodec mpeg4 -sameq -y -t 60 /media/z/.cam/screen/screen_`date +%F_%T`.avi;done;

Works for me, repeats every 60 seconds, creates a new file every 60 seconds.
Problem must be something else.

Offline

#13 2011-03-23 10:02:25

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Bash script: Loop command,

Sorry I wasn't around much lately,

I had a typing error, it work like a charm,

Edit: Only issue is that I can not close the process via a "pkill ffmpeg" - even the ctrl+c on the terminal while the script is working that doesn't stop it?

However if i launch the command on a terminal and then run the "pkill ffmpeg" on another it does stop, so I tried a "pkill desktop-test3.sh" (name of the script, but then again it doesn't stop..


Man many thanks for your help smile

Regards

Last edited by sweetthdevil (2011-03-23 10:18:25)

Offline

#14 2011-03-23 15:11:21

freak
Member
Registered: 2009-04-15
Posts: 17

Re: Bash script: Loop command,

you might want a trap for that. Something like...

while true; do
  ffmpeg -s 2720x1024 -r 25 -f x11grab -i :0.0 -acodec libmp3lame -ab 128k -vcodec mpeg4 -sameq -y -t 60 /media/z/.cam/screen/screen_$(date +%F_%T).avi &
  pid=$!
  trap "kill '$pid'; exit" 1 2 15
  wait
done

Last edited by freak (2011-03-23 15:11:41)

Offline

#15 2011-03-23 18:01:48

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Bash script: Loop command,

so with that addition to the script what pkill command or other command should I use to terminate it?

Offline

#16 2011-03-23 18:06:46

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Bash script: Loop command,

sweetthdevil wrote:

so with that addition to the script what pkill command or other command should I use to terminate it?

I suggest, before you ask these questions, that you do a bit of experimentation.  Try pkill ffmpeg.  Did it work?  Try pkill scriptname.sh - did it work?  Put some effort in.

Offline

#17 2011-03-23 18:13:32

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Bash script: Loop command,

Sorry I should have mentioned it, I did try those two option without success...

pkill ffmpeg do not work what so ever, it just restart the process from the begining (i.e. starting a new record) but doing a pkill new.process work after 2 or 3 "pkill name.process" shouldn't it work the first time?

Offline

#18 2011-03-27 00:29:02

DrZaius
Member
Registered: 2008-01-02
Posts: 193

Re: Bash script: Loop command,

sweetthdevil wrote:
ffmpeg -s 2720x1024 -r 25 -f x11grab -i :0.0 -acodec libmp3lame -ab 128k -vcodec mpeg4 -sameq -y -t 60 /media/z/.cam/screen/screen_`date +%F_%T`.avi 

I recommend using the FFmpeg commands shown here:
How to do Proper Screencasts on Linux Using FFmpeg

It's a well-written guide and the author, verb3k, keeps it up to date which is essential when working with FFmpeg.  Compared to your command the output will probably look better. The command from the guide will also probably be closer to your requested frame rate because the lossless output should be easier for the CPU to deal with and the command utilizes the -threads option.  Lastly, the -sameq option should not be used. The documentation is misleading and should read something like, "use same quantizer as source" instead of "use same video quality as source". Use of the same quantizer does not mean same quality, and this option especially should not be used when the source and output formats do not share the same quantizer scale.

Last edited by DrZaius (2011-03-27 00:31:46)

Offline

Board footer

Powered by FluxBB