How to convert YUYV to H.264 and record?

Hello everyone!
I would like to ask how to convert YUYV to H264 format and record with gstreamer v4l2?
My device is Jetson Nano, USB camera.
I just started to learn about NV devices, and my experience is still superficial, and I hope you can give me guidance

Hi,
Please refer to FAQ
Q: I have a USB camera. How can I lauch it on Jetson Nano?

If you are able to see preview, you can replace nvoverlaysink with

nvv4l2h264enc ! h264parse ! qtmux ! filesink location=a.mp4

to get a mp4 file.

I tried to use your method and still got an error message.

jerry@jerry-desktop:~$ gst-launch-1.0 v4l2src device=/dev/video1 ! 'video/x-raw, format=YUV2, width=640, height=480, framerate=30/1' ! nvv4l2h264enc ! h264parse ! qtmux ! filesink location=record_01.mp4
WARNING: erroneous pipeline: could not link v4l2src0 to nvv4l2h264enc0, neither element can handle caps video/x-raw, format=(string)YUV2, width=(int)640, height=(int)480, framerate=(fraction)30/1

But I have installed nnv4l2h264enc encoder

jerry@jerry-desktop:~$ gst-inspect-1.0 | grep 264
typefindfunctions: video/x-h264: h264, x264, 264
rtp: rtph264depay: RTP H264 depayloader
rtp: rtph264pay: RTP H264 payloader
libav: avenc_h264_omx: libav OpenMAX IL H.264 video encoder encoder
libav: avdec_h264: libav H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 decoder
libav: avmux_ipod: libav iPod H.264 MP4 (MPEG-4 Part 14) muxer
videoparsersbad: h264parse: H.264 parser
omx: omxh264dec: OpenMAX H.264 Video Decoder
omx: omxh264enc: OpenMAX H.264 Video Encoder
x264: x264enc: x264enc
uvch264: uvch264mjpgdemux: UVC H264 MJPG Demuxer
uvch264: uvch264src: UVC H264 Source
nvvideo4linux2: nvv4l2h264enc: V4L2 H.264 Encoder

nvv4l2h264enc expects its input in NVMM memory, so use nvvidconv:

gst-launch-1.0 v4l2src device=/dev/video1 ! video/x-raw, format=YUV2, width=640, height=480, framerate=30/1 ! nvvidconv ! nvv4l2h264enc ! h264parse ! qtmux ! filesink location=record_01.mp4

Hi, sir.
thank you for the method.
I try to test with your method that can produce record.mp4.
but there will be a warning message(this file contains no playable streams) when playing the video.
my instructions are as follow:

jerry@jerry-desktop:~ gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw, width=640, height=480, framerate=30/1 ! nvvidconv ! nvv4l2h264enc ! h264parse ! qtmux ! filesink location=record_01.mp4 Setting pipeline to PAUSED ... Opening in BLOCKING MODE Pipeline is live and does not need PREROLL ... Setting pipeline to PLAYING ... New clock: GstSystemClock Redistribute latency... NvMMLiteOpen : Block : BlockType = 4 ===== NVMEDIA: NVENC ===== NvMMLiteBlockCreate : Block : BlockType = 4 H264: Profile = 66, Level = 0 ^Chandling interrupt. Interrupt: Stopping pipeline ... Execution ended after 0:03:38.576585606 Setting pipeline to PAUSED ... Setting pipeline to READY ... jerry@jerry-desktop:~

is this problem with the video format?

Hi,
If the file can be played, you may ignore the warning.

If there is concern, you can inspect the mp4 file through MediaInfo:
https://mediaarea.net/en/MediaInfo

You may also use EOS so that it properly closes when you interrupt with Ctrl-C:

gst-launch-1.0 -e v4l2src device=/dev/video1 ! video/x-raw, format=YUV2, width=640, height=480, framerate=30/1 ! nvvidconv ! nvv4l2h264enc ! h264parse ! qtmux ! filesink location=record_01.mp4

I have used mediainfo to compare 2 mp4 files.
I found that the mp4 files generated by nvv4l2h264 conversion have no video format information.
Is this the reason why it cannot be played?
If yes, how can I add the video format information?


In addition, I can already use

gst-launch-1.0 v4l2src device=/dev/video0 ! xvimagesink

to see the image

Hi,
On r32.3.1/Nano + Logitech C615 camera, we don’t observe any issue.

nvidia@nvidia-desktop:~$ gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=300 ! video/x-raw,width=640,height=480,framerate=30/1,format=YUY2 ! nvvidconv ! nvv4l2h264enc ! h264parse ! qtmux ! filesink location=a.mp4
Setting pipeline to PAUSED ...
Opening in BLOCKING MODE
Pipeline is live and does not need PREROLL ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Redistribute latency...
NvMMLiteOpen : Block : BlockType = 4
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4
H264: Profile = 66, Level = 0
Got EOS from element "pipeline0".
Execution ended after 0:00:10.210451502
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...
nvidia@nvidia-desktop:~$ gst-launch-1.0 uridecodebin uri=file:///home/nvidia/a.mp4 ! nvoverlaysink
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
Opening in BLOCKING MODE
NvMMLiteOpen : Block : BlockType = 261
NVMEDIA: Reading vendor.tegra.display-size : status: 6
NvMMLiteBlockCreate : Block : BlockType = 261
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Got EOS from element "pipeline0".
Execution ended after 0:00:10.153372662
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...

FYR. You may compare to this working case and investigate further.

Hi,
I think I have found the problem.
in the previous command, i did not set num-buffer of the recording, and the resulting mp4 file was generated in an interrupted manner.
This method should make the file format incorrect, so it cannot be played.
after importing num-buffer settings the way to generate mp4 files will be correct.

Anyway, thank you for your help.

2 Likes