LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2012, 11:21 PM   #1
jamtat
Member
 
Registered: Oct 2004
Distribution: Debian/Ubuntu, Arch, Gentoo, Void
Posts: 138

Rep: Reputation: 24
Help understanding and possibly improving a script I found


Hi. I'm having some troubles recently with my screencasting software solutions: both recordmydesktop and ffmpeg's x11grab, which had both been functioning fine for me, now put audio and video slightly out of sync. I've done a fair amount of searching for solutions and have tried a number of suggested tweaks, but the out-of-sync problem persists.

Among the things that I've found, the only thing that has really addressed the sync issue and given me an output file that has both audio and video in pretty much perfect sync, is a kludgy-looking script someone cobbled together that gets ffmpeg to record audio and video separately, then join them into an output file. A short test file I made with this script, as I mentioned, had audio and video pretty much perfectly in sync.

The script, which came from the ubuntu forums (http://ubuntuforums.org/showthread.php?t=2003738) follows:
Code:
#!/bin/bash
#vzybilly
#these are temp files
aud="aud.mp3"
vid="vid.mp4"
#grab audio & pid
ffmpeg -f alsa -ac 2 -i plughw:0,0 $aud &
audPID=$!
#grab screen & pid
ffmpeg -f x11grab -s "1366x768" -r "24" -i :0.0 -threads 0 -sameq -an -f mp4 $vid &
vidPID=$!
#wait, till name given (that means stop)
read -p "Stop by giving an Output video name?" out
#stop audio and video with pids
kill -n 2 $audPID
kill -n 2 $vidPID
echo "Saving to $out"
#combine to the target output file
ffmpeg -i $aud -i $vid -acodec copy -vcodec copy "$out"
#purge the temp files
rm $aud
rm $vid
I think I understand what it does, though figuring out how to use it took some experimentation. The weird and not-very-intuitive thing about it is that, in order to stop the recording and cause it to join the audio and video files, you have to just type into the terminal where it's running some file name under which it should save the output; all this while ffmpeg is running and giving you a message that you should type ctrl-c to stop the recording.

So, though effective, the script could definitely use improvement. Seems to me it should begin by asking for a file name, then make that into one of the variables: but my knowledge of scripting is so rudimentary that I don't have the slightest idea how that would be done. Then, the matter of causing the script to go from record mode to join mode seems like it could use help as well.

Can anyone on here offer some improvements to this script that would make it run a bit more coherently? Input will be greatly appreciated since, for the time being, this is the only way I have to produce screencasts that have audio and video in sync.

Thanks,
James
 
Old 09-21-2012, 12:28 AM   #2
Zero Angel
Member
 
Registered: Jul 2010
Distribution: PinguyOS 12.04
Posts: 50

Rep: Reputation: 1
You can always use zenity to create a graphical popup asking to give the file a name

For example replace:
Code:
read -p "Stop by giving an Output video name?" out
with

Code:
out="`zenity --entry --title="Video muxing script" --text="Please give the video file a name"`.mp4"

Last edited by Zero Angel; 09-21-2012 at 12:46 AM.
 
Old 09-21-2012, 12:37 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
(1)
Please note the & in the last column of the two "ffmpeg" commands. They serve to run ffmpeg in the background so that the next xommands they run. As such pressing ctrl+c to kill the two instances will not work. So you might as well redirect them away from the console - 2>/tmp/something. This will avoid unnecessary type ctrl-c to stop messages
which you can't act upon anyway and only serve to obscure genuine error messages that should catch your attention.

(2)
The $aud and $vid are hard coded (to aud.mp3 and vid.mp4). Thus there initially appears to be no need to have them as variables. Except for future

(3)
As it stands, if you invoke the script again while it is already running, it will simply corrupt the file capture (aud.mp3 and vid.mp4). So you might like to put a test to avoid duplicate running.

The script might then become
Code:
#!/bin/bash
#vzybilly
# duplicate instance check 
#these are temp files
aud="aud.mp3"
vid="vid.mp4"
#grab audio & pid
ffmpeg -f alsa -ac 2 -i plughw:0,0 $aud &
audPID=$!
#grab screen & pid
ffmpeg -f x11grab -s "1366x768" -r "24" -i :0.0 -threads 0 -sameq -an -f mp4 $vid 2>/tmp &
vidPID=$!
#wait, till name given (that means stop)
read -p "Stop by giving an Output video name?" out
#stop audio and video with pids
kill -n 2 $audPID
kill -n 2 $vidPID
echo "Saving to $out"
#combine to the target output file
ffmpeg -i $aud -i $vid -acodec copy -vcodec copy "$out"
#purge the temp files
rm $aud
rm $vid
I will work on the duplicate check (usually you grep the process status for instances of ffmpeg, exclude your grep and if the line count is > 0, then it is supposed to be running already).
 
Old 09-21-2012, 02:03 PM   #4
jamtat
Member
 
Registered: Oct 2004
Distribution: Debian/Ubuntu, Arch, Gentoo, Void
Posts: 138

Original Poster
Rep: Reputation: 24
When I add
Code:
2>/tmp
in the place stipulated, then run the script, I get the following error message:
Quote:
vid.mp4: No such file or directory
rm: cannot remove `vid.mp4': No such file or directory
I guess that means the transitional video file ffmpeg was creating got somehow sent into oblivion by the addition of that bit?

I would be much obliged if you could provide for me some sort of duplicate check. That does sound like an improvement.

Thanks for the input,
James
 
Old 09-21-2012, 02:21 PM   #5
jamtat
Member
 
Registered: Oct 2004
Distribution: Debian/Ubuntu, Arch, Gentoo, Void
Posts: 138

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by Zero Angel View Post
You can always use zenity to create a graphical popup asking to give the file a name

For example replace:
Code:
read -p "Stop by giving an Output video name?" out
with

Code:
out="`zenity --entry --title="Video muxing script" --text="Please give the video file a name"`.mp4"
Thanks, Zero Angel. After I installed zenity I was able to try this and it does give an interesting result: a pop-up window appears over the terminal and, once a name is entered into that window the recording process stops and the joining of audio with video begins. Definitely an improvement.

James
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] improving a sed script cruzdelsur Programming 12 08-27-2010 09:59 AM
Improving an existing script kaplan71 Linux - Software 4 10-23-2009 07:40 PM
Understanding a Shell Script I Found carlosinfl Linux - General 1 07-11-2007 06:18 PM
improving my script lpn1160 Linux - Newbie 4 06-19-2006 08:09 PM
Improving the startx script Woodsman Slackware 14 11-05-2005 08:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration