8 Replies Latest reply on Nov 23, 2016 7:13 AM by ignas_kaz

    High Quality (HD) Audio and Video Recorder using the Raspberry Pi


      Hello all. I'm new to the Raspberry Pi (just got my first Pi, an RPi 3 Model B). I am looking to create an high-quality audio+video recorder out of my Raspberry Pi. (In a way, I'm basically trying to make a camcorder.) I want to add the Raspberry Pi Camera Module and the Cirrus Logic Audio Card to my Raspberry Pi. I require very-high quality recording.


      I want to feed my line-level audio source (coming from my mixer via a 1/4" -> 3.5mm adapter) to the Cirrus Logic Audio Card line-level (pink) input. I'd like to also like to have a Raspberry Pi camera module connected and recording at the same time. I'd like to simultaneously record 1080p 30fps video from the camera module and 24-bit 96kHz audio from with the Cirrus Logic Audio Card. In other words, I'm looking to record very-high quality audio and video at the same time. I'd like both recordings to be in sync (in terms of the exact moment they start) and exactly the same length (in terms of the exact moment they stop). The audio and video do *not* have to be contained in a single file.


      I've read about people recording high quality video or high quality audio, but haven't seen anyone doing it at the same time. My primary concern is whethter or not the Raspberry Pi will be able to process this much information in real-time, and have the ability to write all this information to my SD card fast enough (I happen to be using a Samsung Evo 64 GB Class 10 Mirco SD card). I know from hacking my DSLR camera for video using Magic Lantern software, the place where things fall apart on those systems is having enough speed writing to the card.


      Has anyone tried to turn the Raspberry Pi into such a high-quality recorder? Were you able to record very high quality without any problems?


      My goal is to eliminate (most) post-production work. If I could record audio and video directly to a Raspberry Pi, that would just about get me there for my needs. I know I can record to an external recorder (like a Tascam DR-05 audio recorder or an external HDMI video recorder, both of which I already own), but this would be so much easier.



        • Re: High Quality (HD) Audio and Video Recorder using the Raspberry Pi

          Hi Tom,


          Given the paper specifications of the Pi 3, I see no reason why you couldn't record audio and video simultaneously, both in reasonable quality (well, high quality for the audio, and reasonable quality for the video since the camera is not broadcast quality).

          The audio could be reasonably in sync for most people's needs I would have thought. For broadcast quality you'd possibly need to devise

          a way to get time-codes embedded in the audio and video. I doubt anyone has done that for the Pi (or at least not openly documented it

          perhaps, so you might be trailblazing in that area. It shouldn't be impossible given there are specifications for such things, but it is work effort

          that would need to be done by a software engineer.

          • Re: High Quality (HD) Audio and Video Recorder using the Raspberry Pi

            Hi, thanks for posting this idea, it's very interesting indeed. Please post a follow up here, if you manage to find a way to do this so that we could all learn from this I'd love to try this setup w/ my RPI3 that already has the camera module..

            • Re: High Quality (HD) Audio and Video Recorder using the Raspberry Pi

              I have not tested resolutions higher than 720p but intend to do that soon. I am working on audio/video sync though. At the moment my requirements are not as stringent as yours, but I think 1080p30 video with 24-bit 96 kHz audio is possible, especially on the RPi 3. Recording audio and video at the same time is relatively simple. Here are the two key lines from my script:

              raspivid -t ${video_length} -n -w 1280 -h 720 -fps 25 -o ${video_file}.h264 &

              arecord -Dhw:sndrpiwsp -d ${audio_length} -c 2 -f s16_LE -r 8000 > ${audio_file}.wav


              As you can see, my resolution is 1280x720 with 25 fps, and the audio is only 16-bit with 8 kHz. The trick is synchronization after recording. Apparently the RPi camera has a design flaw with an oscillator of the wrong frequency, so if you do -fps 25 the result is not exactly 25.000 Hz. I have not nailed this down yet to get perfectly synchronized video/audio every time, but I have had some success using avconv to adjust the frame rate and have fixed some videos very nicely:

              avconv -i ${video_file}.h264 -f rawvideo - | avconv -y -f rawvideo -r:v 25.2 -s:v 1280x720 -i - -b 2000k ${video_file_25fps}.mov


              Of course, that is not going to be the quality you want, but I would imagine you could adjust the different parameters to maintain the highest possible quality of your original 1080p30 recording. NOTE: I do this avconv command on a desktop computer because it is an incredible CPU hog. Basically it converts h264 to raw video and then on the fly (hence the pipe) converts to MPEG format forcing the correct frame rate. My RPi B+ reports "1 fps" processing speed but it's really less than that, so I've never attempted to let this go to completion on the RPi. So, after transferring the original .h264 video to my desktop, adjusting the frame rate, then sending the new file back to the RPi, I merge the audio and video like so, forcing the originally intended 25 fps in the final file:

              avconv -y -fflags nobuffer -r 25.2 -i ${video_file_25fps}.mov -fflags nobuffer -analyzeduration 0 -i ${audio_file}.wav -map 0:0 -map 1:0 -filter:a aresample=async=1 -c:a copy -c:v copy -r 25 ${video_audio_sync_file}.mov

              You could probably do all the "post-processing and sync" stuff on a desktop machine. I send the adjusted frame rate video back to the RPi only because I could not get the exact version of avconv on my other system, and it fails to perform the synchronizing command, but the version of the RPi works! By the way, I recently upgraded to the latest version of Raspbian jessie.


              I cannot take credit for any of this really, so below are my sources. Also, I cannot explain ALL of the options used in these commands. I think I understand most of them, but I'm definitely not sure about some. There may be some that are not necessary, but for the moment I'm sticking with what works. Also, keep in mind that in addition to the weird "25 fps is actually 25.2 fps" problem (by the way, your camera's oscillator may not result in exactly 25.2 fps, so some experimenting may be needed), the audio recording may be slightly delayed, which would require an additional option for the final sync command (Google avconv -itsoffset). I'm hoping that with the RPi 3 (still waiting for mine to arrive), any lag between starting raspivid and arecord will be insignificant (my RPi B+ is WAY slower than the RPi 3), and the only problem remaining on the RPi 3 will be adjusting the video frame rate (the oscillator flaw sounds like a hardware problem). Possibly shutting down a lot of system services that are enabled by default in Raspbian could also help to minimize the audio recording delay, especially with your application. For example, you probably won't need any network services. However, I have not looked into this optimization step yet.


              As for the SD card, I read lots of reviews, and settled on the same Samsung card you mentioned, just the 16 GB version as I'll be periodically dumping my videos to a file server over the network. However, at the moment I am still using the SD card that came with my RPi, and I don't know its specs. If my calculations are correct, I think 1080p30 h264 video from the RPi camera would require roughly 2 MB/s, so the SD card write speed should definitely handle that. One other thing to consider though: I have read the XFS filesystem is critical to achieve optimal SD card performance. I have the overrun error mentioned in the thread linked below, but I have not had time to test XFS yet to see if it is the right solution for me as well. I inherited this RPi when I joined my research project, so I'm still trying to dig up all the details about how the PhD student who set up the RPi configured everything.



              Recording video and audio commands: RPi docs

              Discussion of RPi camera oscillator flaw: https://www.raspberrypi.org/forums/viewtopic.php?t=98541&p=683936 (This stuff goes way over my head at one point, and you'll see at the end I was asking for help, but unfortunately I never received a reply. You'll see my script as it was back then. Since then I abandoned MP4Box conversion in favor of the avconv "frame rate fix" command mentioned above and referenced in the next link.)

              Adjust frame rate: raspbian - raspivid not saving metadata - Raspberry Pi Stack Exchange

              Merge audio/video: Sync Audio & Video From Pi Camera & USB Microphone - Raspberry Pi Stack Exchange

              SD card performance (XFS): https://www.raspberrypi.org/forums/viewtopic.php?t=43738&p=3489d42

              3 of 3 people found this helpful
              • Re: High Quality (HD) Audio and Video Recorder using the Raspberry Pi

                Hey guys,


                Just an update on audio. I finally got the audio card working after some struggle which I documented in another discussion. Recording on the line-in port (the pink one) works great and sounds very clean. The analog to digital conversion also sounds very good to my ears.


                Just a few quick notes:


                1. The sound card only supports the following formats when recording PCM audio (a .wav file):


                • S16_LE
                • S24_LE
                • S32_LE


                The "S" stands for signed integers and the "LE" stands for little endian (this is pretty standard stuff and should only matter if you're doing weird low-level audio signal programming). You can record at a bit depth of 16, 24, or 32. Floating is not supported. Low-quality 8-bit recording is not supported. If you're using arecord, you'll have to explicitly specify one of the three formats above using the -f flag, as the default for arecord is 8 bits which, again, is not supported.


                2. The audio card does not support mono recording, only stereo. Again, you have to specify this to arecord because the default is mono.



                Here's my basic command to record. I sample at 96,000 Hz, which is good for high quality audio.


                $ arecord -f S32_LE -r 96000 -c 2 myRecording.wav



                Next up is getting video recording to work well.

                1 of 1 people found this helpful
                • Re: High Quality (HD) Audio and Video Recorder using the Raspberry Pi

                  Hey everyone,

                  the tool picam may be a good option for your recording needs - it works quite well for me. I'm using a very cheap USB microphone but it should also work with any other input you can access through alsamixer.

                  For reference - I put some ressources I could find on audio and video recording and streaming together here. It's very beginner-friendly but may be some help for you as well.

                  Cheers, Daniel

                  1 of 1 people found this helpful
                  • Re: High Quality (HD) Audio and Video Recorder using the Raspberry Pi

                    Hi everyone,

                    During my research on internet for my project, i stumbled on this thread. Thou I have a bit different needs than Tom, we do shear some of similar issues. I need to make a high quality sound recorder but for wider frequency range (from ~40Hz to  ~40kHz) and video recording is just optional. I'm intending to record underwater sounds of pinnipeds for my studies. The input for recorder can be preamplified if need be. But my question is: is there any boar to have audio input for raspberry apart from cirrus logic audio card?

                    Any and all information would be much appreciated


                    P.S. I hope that my question is understandable and sorry for my English