Author Topic: Record Audio & Video from Webcam  (Read 3429 times)

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3747
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Record Audio & Video from Webcam
« on: January 29, 2012, 03:22:00 AM »
Isolated from Kill a Process ... some help needed

Thanks for that addition  ;)

I have now - I hope - finished my wee project.

I had a problem recording from webcam while keeping the sync correct and not dropping too many frames and such problems.

I am now recording audio and video separately, muxing them, and then converting the result to mp4 (HQ).

Presently I am using a mix of mencoder for the recording and muxing and ffmeg for the converting.
I might sometime change to use only one of them .......  but I always have both installed so it makes little difference to me.

I doubt anyone is interested but if so here is a link to the script.

http://dl.dropbox.com/u/5838889/CamRecord-0.1.sh

Some of the settings may need to be changed depending on your webcam ......

Thanks for the help  ;)

Another wee itch scratched!  :D  :D

regards

EDIT:  Oops!   Just realised I forgot to mention Leiche ......  it was from his script for a similar purpose that I started. My apologies for not stating this earlier.  :(   Thanks Leiche  ;)




Glad to hear, that it work good? perfectly? for you  :)
I get here no sync (thinking: my webcam is the troublemaker )...
« Last Edit: January 30, 2012, 12:56:27 PM by Leiche »

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #1 on: January 29, 2012, 03:56:38 AM »
I found I had to choose a particular fps for the video and no convertion when recording.
So I have recorded in raw (YUY2 in my case), recorded the audio separately, muxed the two and then converted.

Yes it is inconvenient ..... and much longer than should be necessary ....  but the results are reliable.

Hopefully I will stumble on a better scheme in the future.

There is no reason I can think of, that it could not all be done in one operation on this 4 core machine.

Well there is one thing ....  and I know nothing about it .......  apparently Alsa has a buffer size set somewhere.
I think it would be worthwhile to change that to see if it makes a difference with the sync problem.

BTW .....  I did get it done in one command and each recording had the same out of sync time ......  about 1.5 secs. The variation was less than 0.1 secs IIRC.

If I could figure out why that was so then all would be well.

For the moment .....  to take care of the few recordings I need .....  the present arrangement will do. I can let it process away in the background after the recording itself is completed.

Still a work-in-progress  ;)

EDIT:
            Leiche .......  I wonder how this command works on your system ........

Code: [Select]
ffmpeg -f video4linux2 -i /dev/video0 -r 30  -vcodec mpeg4 -b 1000 -qscale 0.1 -bt 256k -y -f alsa -ac 1 -i hw:1,0  -ar 44100 ffmpeg-`date +%s`.mp4
The qscale is extremely high above .....  I was testing how high I could make it. Maybe a value of 2.0 or 3.0 would be good enough.

Please let me know if you try it  ;)
This is the one with the consistent sync offset.

regards
« Last Edit: January 29, 2012, 04:15:28 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #2 on: January 29, 2012, 09:39:21 AM »
So using the ffmpeg command below I have a 1500ms sync issue

Code: [Select]
ffmpeg -f video4linux2 -i /dev/video0 -r 30  -vcodec mpeg4 -b 1000 -qscale 0.1 -bt 256k -y -f alsa -ac 1 -i hw:1,0  -ar 44100 ffmpeg.mp4
I had tried this on many occasions and continued to get the same sync .....  and was easily able to correct it using Avidemux-QT.

So I had a look at the cli version of Avidemux and added the following command to correct the ffmpeg sync issue .........

Code: [Select]
/usr/bin/avidemux2_cli  --load ffmpeg.mp4 --audio-delay +1500 --audio-codec copy --video-codec copy --output-format MP4 --save avidemux.mp4
I am quite happy with the results.

Briefly looking at the workings of Avidemux-cli it appears that there is extra video data in the file which might be causing the sync problem.
Watching the ffmpeg video, the first part does not display but the audio plays.
It appears this might be where the 'extra' data is.

Code: [Select]
The video codec has some extradata (46 bytes)

0000 : ...�....��......  00 00 01 b0 01 00 00 01 b5 89 13 00 00 01 00 00
 0010 : .. .č�.�..<.c..  00 01 20 00 c4 8d 88 00 f5 14 04 3c 14 63 00 00
 0020 : .�Lavc52.123.0  01 b2 4c 61 76 63 35 32 2e 31 32 33 2e 30
Deleting post proc
Initializing postproc

Code: [Select]
Encoding Phase        : Encoding
We have extradata for video in copy mode (46)

So if the ffmpeg file could be saved without this extra data then maybe all would be OK.

Maybe it is caused by the slow wake up of the webcam .....  only a guess ....  maybe I will find something somewhere to indicate what might be the cause.

Comment:
                 I have brought this thread waaay off topic ...... would a mod consider breaking the video aspect (after the script problem was resolved) into another thread?  Thanks
« Last Edit: January 29, 2012, 09:42:51 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #3 on: January 29, 2012, 09:44:40 AM »
Reply number?     

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #4 on: January 29, 2012, 10:08:36 AM »
Reply number?    

Leave your reply #40 .......  ? ....  seems about right I think ........  thanks  ;)
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3747
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #5 on: January 29, 2012, 10:32:11 AM »


EDIT:
            Leiche .......  I wonder how this command works on your system ........

Code: [Select]
ffmpeg -f video4linux2 -i /dev/video0 -r 30  -vcodec mpeg4 -b 1000 -qscale 0.1 -bt 256k -y -f alsa -ac 1 -i hw:1,0  -ar 44100 ffmpeg-`date +%s`.mp4


Please let me know if you try it  ;)
This is the one with the consistent sync offset.

regards

Get this error
Code: [Select]
[alsa @ 0xa5d360] cannot set channel count to 1 (Invalid argument)changed hw from 1.0 to 0.0, and to 0.1 no luck here  ::)

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #6 on: January 29, 2012, 12:03:31 PM »
Code: [Select]
ffmpeg -f video4linux2 -i /dev/video0 -r 30  -vcodec mpeg4 -b 1000 -qscale 0.1 -bt 256k -y -f alsa -ac 2 -i hw:1,0  -ar 44100 ffmpeg-`date +%s`.mp4
I changed the audio channels to 2 ...... which is probably what your system needs ......
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #7 on: January 29, 2012, 12:07:04 PM »
I also came across this Gstreamer command .......  which saves the audio + video in RAW and needs converting afterwards .....

Code: [Select]
gst-launch-0.10 v4l2src ! 'video/x-raw-yuv,width=640,height=480,framerate=30/1' ! \
tee name=t_vid ! queue ! videoflip method=horizontal-flip ! \
xvimagesink sync=false t_vid. ! queue ! \
videorate ! 'video/x-raw-yuv,framerate=30/1' ! queue ! mux. \
alsasrc device=hw:1,0 ! audio/x-raw-int,rate=48000,channels=1,depth=16 ! queue ! \
audioconvert ! queue ! mux. avimux name=mux ! filesink location=gst.avi

It of course produces huge files .......  but the ffmpeg command above does a decent job of converting it - here.

Maybe the idea of using tee could be adapted for some of the other commands which have sync problems ..

regards
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3747
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #8 on: January 29, 2012, 12:11:57 PM »
Code: [Select]
ffmpeg -f video4linux2 -i /dev/video0 -r 30  -vcodec mpeg4 -b 1000 -qscale 0.1 -bt 256k -y -f alsa -ac 2 -i hw:1,0  -ar 44100 ffmpeg-`date +%s`.mp4
I changed the audio channels to 2 ...... which is probably what your system needs ......

After changed again hw it's work. I will test something with your code.
Thanks for it...

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #9 on: January 29, 2012, 01:34:29 PM »
It is a pity that Kamoso does not have the facility to edit a lot of its settings ......  or indeed that we do not yet have GuvcViewer in the repo .......  which from all I have read has a lot of possibilities.

Let me know Leiche if that last command does a good job or not  ;)

Note:  Change the 'qscale' option to maybe 4.5 or so, for reasonable sized files with good (for me) quality.
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: [ SOLVED ] Kill a Process ... some help needed
« Reply #10 on: January 29, 2012, 04:52:46 PM »
Reply number?    

Leave your reply #40 .......  ? ....  seems about right I think ........  thanks  ;)
     
Done. :D     

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: Record Audio & Video from Webcam
« Reply #11 on: January 30, 2012, 04:15:26 AM »
Reply number?    

Leave your reply #40 .......  ? ....  seems about right I think ........  thanks  ;)
     
Done. :D     

Thanks Neal ;) ......  Can you change the title to something related to this discussion? ......  or maybe Leiche can do it .... as the first post is now his.

regards
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Record Audio & Video from Webcam
« Reply #12 on: January 30, 2012, 04:30:44 AM »
Reply number?    

Leave your reply #40 .......  ? ....  seems about right I think ........  thanks  ;)
     
Done. :D     

Thanks Neal ;) ......  Can you change the title to something related to this discussion? ......  or maybe Leiche can do it .... as the first post is now his.

regards
     
Lets let Leiche do it. ;D     

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3747
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Re: Record Audio & Video from Webcam
« Reply #13 on: January 30, 2012, 12:57:03 PM »
Done...

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10686
  • MLUs Forever!
Re: Record Audio & Video from Webcam
« Reply #14 on: February 01, 2012, 09:04:04 AM »
Leiche, have you played with Guvcview in 64 bit?

Only thing that seems to be missing from its controls is the ability to record audio without video.

Also it seems to expect the camera to be /dev/video0 and no other.

But as for video codecs it seems OK, and can output in avi and mkv formats.

I would like to have this one in the standard repo ....  but I guess there is some reason it is not there.

Anyway, just wondered if you had used it  ;)
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT