Skip to content

Instantly share code, notes, and snippets.

@jetsonhacks
Last active September 8, 2019 17:30
Show Gist options
  • Save jetsonhacks/4fcedcb00a3ca3389b76 to your computer and use it in GitHub Desktop.
Save jetsonhacks/4fcedcb00a3ca3389b76 to your computer and use it in GitHub Desktop.
Using Gstreamer, take a h264 stream from a Logitech c920, preview it on the display and save it to a file along with the webcam audio.
#!/bin/sh
# Preview and save webcam video on NVIDIA Jetson TK1
# Grab audio and video (in h264 format) from Logitech c920 @ 1920x1080
# Preview @ 1280x720 on screen
# Store video to file named gEncode1080p.mp4
# Logitech c920 is video1 on this machine
VELEM="v4l2src device=/dev/video1 do-timestamp=true"
# Video capability from the camera - get h264 1920x1080
VCAPS="video/x-h264, width=1920, height=1080, framerate=30/1"
# Video Source
VSOURCE="$VELEM ! $VCAPS"
# Decode the video - parse the h264 from the camera and then decode it
# Hardware accelerated by using omxh264dec
VIDEO_DEC="h264parse ! omxh264dec"
# SIZE OF THE PREVIEW WINDOW ON THE DISPLAY ; 1280x720 here
PREVIEW_SCALE="video/x-raw, width=1280, height=720"
# VIDEO_SINK is the preview window
VIDEO_SINK="videoconvert ! videoscale ! $PREVIEW_SCALE ! xvimagesink sync=false"
FILE_SINK="filesink location=gEncode1080p.mp4"
# ============ AUDIO ===========================
# Use $ pctl list
# to get the device ;
# alsasrc had issues, so pulsesrc was used instead
AELEM="pulsesrc device=alsa_input.usb-046d_HD_Pro_Webcam_C920_A116B66F-02-C920.iec958-stereo do-timestamp=true"
AUDIO_CAPS="audio/x-raw"
AUDIO_ENC="audioconvert ! voaacenc"
ASOURCE="$AELEM ! $AUDIO_CAPS"
#show the gst-launch command line; can be useful for debugging
echo gst-launch-1.0 -vvv -e \
mp4mux name=filemux ! $FILE_SINK \
$VSOURCE ! tee name=tsplit \
! queue ! $VIDEO_DEC ! $VIDEO_SINK tsplit. \
! queue ! h264parse ! filemux.video_0 \
$ASOURCE ! queue ! $AUDIO_ENC ! filemux.audio_0
# mp4mux filemux is the multiplexer for the file that's being written
# Video and Audio source go into the filemux
# Video also goes into the VIDEO_SINK, which is a window on the display
gst-launch-1.0 -vvv -e \
mp4mux name=filemux ! $FILE_SINK \
$VSOURCE ! tee name=tsplit \
! queue ! $VIDEO_DEC ! $VIDEO_SINK tsplit. \
! queue ! h264parse ! filemux.video_0 \
$ASOURCE ! queue ! $AUDIO_ENC ! filemux.audio_0
@chickenrun525
Copy link

chickenrun525 commented May 31, 2017

Hello JetsonHacks,

I am new to gstreamer. I am trying to record audio and video from logitech c920 on Ubuntu. I used the following pipeline to record video. Instead of using v4l2src, I choose uvch264src to set the bitrate (as far as I know, v4l2src doesn't support changing bitrate).

"gst-launch-1.0 -v -e uvch264src initial-bitrate=10000000 average-bitrate=10000000 iframe-period=1000 device=/dev/video1 name=src auto-start=true src.vfsrc ! queue ! video/x-raw,format=(string)YUY2,width=320,height=240,framerate=10/1 ! xvimagesink sync=false src.vidsrc ! queue ! video/x-h264,width=1280,height=720,framerate=30/1 ! h264parse ! flvmux ! filesink location=test.flv"

Then, I tried to record the audio as well. Based on your pipeline, I tried to add the audio recording part to my pipeline. Unfortunately, I cannot make it work. When I run the following pipeline, the preview shows a frame and then stuck there.

"gst-launch-1.0 -v -e uvch264src initial-bitrate=10000000 average-bitrate=10000000 iframe-period=1000 device=/dev/video1 name=src auto-start=true src.vfsrc ! queue ! video/x-raw,format=(string)YUY2,width=320,height=240,framerate=10/1 ! xvimagesink sync=false src.vidsrc ! queue ! video/x-h264,width=1280,height=720,framerate=30/1 ! h264parse ! muxout. pulsesrc device="alsa_input.usb-046d_HD_Pro_Webcam_C920_511596BF-02-C920.analog-stereo" do-timestamp=true ! audio/x-raw ! queue ! audioconvert ! voaacenc bitrate=65536 ! muxout. flvmux name=muxout ! queue ! filesink location=~/Videos/test2.flv "

My gstreamer version:
gst-launch-1.0 version 1.2.4
GStreamer 1.2.4

Could you please help me take a look and offer some help?
Thanks in advance.

@sumejko92
Copy link

sumejko92 commented Aug 31, 2017

Hello chickenrun525,
I am trying to do the same. I have a logitech c920 and I am usung Ubuntu 14.04 but when I run your command I get a static image displayed (not a video stream) and nothing is saved into the file location. I see a lot of ways of doing this with gstreamer-0.10 and this example for gstreamer-1.0 is not working. I am interested only in the video, so I dont need the audio part.

My gstreamer version:
gst-launch-1.0 version 1.2.4
GStreamer 1.2.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment