Encode Video Once on the GPU for Both Near-Live View and Recording
This is an English translation of the Japanese original, made with an LLM (Claude Opus 5.5). If anything is unclear, the Japanese version is authoritative. Read the Japanese original
For the Japanese original, the author planned the article and checked its content and references, and an LLM (OpenAI Codex) was used to organize the structure and write the text. The author is responsible for the content.
Suppose you are building a small prototype that analyzes a video stream.
The video coming from a camera is decoded on the GPU and processed, for example to detect objects. The results are saved as numbers and coordinates, but the analyzed video is also saved so the results can be checked later.
You also want to watch the current video in a browser to check that the processing works correctly. This is not a service that streams video to an unspecified audience. The person who set up the system opens the screen now and then to check its state. Viewers are few, and a delay of a few seconds is acceptable.
For this use, let's consider how to produce video for live viewing and for long-term storage.
Encode only once on the GPU
NVIDIA GPUs have NVDEC for decoding video and NVENC for encoding it. Decoded frames can be processed on the GPU and passed to the dedicated encoder.
NVIDIA Video Codec SDK: https://docs.nvidia.com/video-technologies/video-codec-sdk/13.1/index.html
If you only analyze the input video, you could also save the original compressed data as it is. But if you mask detected objects or change the resolution for display, the modified frames must be encoded again.
There is no need to do this encoding separately for live viewing and for recording.
Camera video stream
↓
Decode on the GPU
↓
Analysis and image processing
↓
Encode once on the GPU
↓
Short video segments
├── Near-live view (short term)
└── Several combined for long-term storage
Using the same encoded output means the live view and the recording do not end up with different quality or resolution. If you mask images, it is done once before the branch, which also makes it easier to avoid accidentally forgetting the mask on one of the paths.
Split into self-contained videos of a few seconds
The encoded output is split into short videos of, for example, 5 to 10 seconds. The length is not fixed; it varies a little depending on where the keyframes are.
Here, each one is a fragmented MP4 that can be played on its own. A single file holds the initialization information needed to start playback and the video data.
Each time a short segment is finalized, it is saved to object storage, and the browser is notified that a new video is available. The browser appends the segments in order to a Media Source Extensions SourceBuffer.
Media Source Extensions: https://www.w3.org/TR/media-source-2/
Because a file is displayed only after it is finalized every few seconds, this is not real-time communication like a video conference. But for checking the processing status of a prototype, near-live viewing a few seconds behind is often enough.
Treat the live view as short-term data
Segments for the near-live view are saved only while someone is watching. The storage location has a short lifecycle rule, and old segments are deleted automatically.
When the browser opens the screen, it first fetches the most recent segments currently stored. After that, it receives notifications of new segments and appends them to the end.
If you rely only on notifications, video is missing for the time the browser was briefly disconnected. Notifications may also be duplicated or arrive out of order. So the setup is:
- First fetch the list of recent segments
- Receive notifications of new segments
- Remove duplicates by a stable ID and sort by recording time
- When a time gap or a disconnection is detected, fetch the list again
Events are used to learn about new segments quickly, and the current state can always be fetched again from storage.
Also, a failure to save or notify for the live view must not stop the analysis or the recording. It would be bad to lose the video meant for long-term storage for the sake of a live screen nobody is watching.
Combine into tens of seconds for long-term storage
You could also store the short near-live segments as they are. But saving 5-second videos all day produces 17,280 objects per day for a single device.
The finer the objects, the more listing requests, search data, signed URLs and playback requests there are. On the other hand, making them several minutes long increases the time until a file is finalized, and the unit to resend when a save fails becomes larger too.
So, for example, several short near-live segments are combined into a recording object of about 30 seconds. The 30 seconds is not a value dictated by the video format. It is chosen as a balance between the number of objects, ease of handling during playback, the delay until finalization and the size of the resend unit.
Here, there is no need to decode and re-encode the video. If the segments share the same codec, resolution, time base and so on, and their times are continuous, FFmpeg's stream copy can combine them into one container.
ffmpeg -f concat -safe 0 -i segments.ffconcat -c copy recording.mp4
-c copy copies the compressed stream into a new container without decoding or encoding. There is no quality loss from re-encoding, and no extra GPU encoding work.
FFmpeg stream copy: https://www.ffmpeg.org/ffmpeg.html#Stream-copy
However, not every file can be combined unconditionally. FFmpeg's concat demuxer assumes that each input has the same streams, codecs, time base and so on.
FFmpeg concat demuxer: https://ffmpeg.org/ffmpeg-formats.html#concat
If the input is disconnected, the resolution or codec changes, time goes backward, or there is a large gap, the recording is finalized there and the next file starts. Rather than adding up nominal fixed values such as 5 seconds, the actual start time and length of each one are recorded.
Success means different things for live and recording
Even though they share the short segments, the live view and long-term storage are not the same process.
The live view is best effort. Even if a little new video is missing, it can continue to be used once it catches up with the latest state. If nobody is watching, saving for the live view can stop altogether.
The recording is data for checking later. If a save fails, it stays in a local spool and is retried. Even if the process restarts, unsent recordings can be restored.
Short encoded segments
├── live queue ── short-term storage ── notification
│ on failure, move on to the next
│
└── recording spool ── stream copy ── long-term storage
kept until it succeeds
Everything up to encoding is shared; the save queue, storage location, retention period, retries and success conditions are separate. This keeps delays in the live view and notification failures from spreading to the recording path.
You can also use AWS video services
AWS has managed services for handling video.
Amazon Kinesis Video Streams can ingest video from devices, store it, and play live or past video. It also supports playback with HLS and MPEG-DASH. It is one of the candidates closest to these requirements.
https://docs.aws.amazon.com/kinesisvideostreams/latest/dg/how-it-works.html
Amazon IVS is a service for delivering low-latency live video to many viewers and building interactive experiences. It manages ingest, transcoding, global delivery and even a dedicated player.
https://docs.aws.amazon.com/ivs/latest/LowLatencyUserGuide/what-is.html
AWS Elemental MediaPackage dynamically packages live video into HLS, DASH and other formats and acts as the delivery origin.
https://docs.aws.amazon.com/mediapackage/latest/userguide/what-is-terms.html
These are strong options when you need many viewers, multiple quality levels, standard delivery protocols, broad device support and less work running the delivery infrastructure.
In this prototype, on the other hand, the analyzed video is already encoded on the GPU and viewers are few. What was needed was not global video delivery but checking saved short videos with a small delay. Time synchronization with the analysis results and producing long-term recordings from the same data were also needed.
In such a case, combining short-term storage, object creation notifications, a CDN and the browser's MSE can let you build only the parts you need.
However, if the number of viewers grows, multiple quality levels are needed, the variety of devices and browsers increases, you want lower latency, or you do not want to run delivery monitoring yourself, it is better to choose a managed service again.
GPU codec support alone does not decide it
Even if NVENC and NVDEC support a codec, the browser may not be able to play the same codec with MSE. You need to choose a codec that works through the entire chain: the input camera, the GPU, the container format, the browser and the devices used.
Even if another codec looks attractive for its compression ratio, it cannot be used for the checking screen if some of the browsers in use cannot play it. In addition to checking with MediaSource.isTypeSupported(), you need to test playback on the actual target devices.
Summary
If you need small-scale near-live checking and long-term recording, one approach is to encode the analyzed video once on the GPU and treat the output as short, self-contained segments.
- For near-live viewing, store the short segments as they are for a short time, and display them as they are finalized
- For long-term recording, combine the same segments into tens of seconds with stream copy
- Separate the live view as best effort and the recording as a retryable persistent spool
- Do not trust event notifications alone; resynchronize from the current list of segments
- When viewers or delivery requirements grow, reconsider managed video services
I think what matters is not producing the video twice for live and recording, but using the video encoded once with different lifetimes and success conditions.