Sunday, July 3, 2022

Time Shifting with a $10 HDMI capture stick

Where it begins

I first heard of the USB HDMI capture stick through HackAday 2 years ago where someone creatively used his phone as a view finder for his digital camera. I didn't have such a use as the connection is rather cumbersome. However, I was intrigued by the USB HDMI capture stick that can be bought for less than $10. So I ordered one.

I have some other idea in mind. I am used to recording TVs using devices like TiVo that gives me the ability to Time Shift. Meaning, I can start the program and let it build up the buffer, go do something else and return and start watching from beginning. The purpose is to skip commercials.

Nowadays, I still watch time shifted TVs OTA (over-the-air) but more and more contents are available via streaming and there seems to be no generic answer to commercial skipping.

Enters the HDMI capture card. The first thing I tried is to ensure I can use it for its intended purpose. I was pleasantly surprised that it is Plug-n-Play. It appears as a webcam in Windows 10 and I can use the built-in Camera app to record video and audio.

Time to start experimenting

The next thing I want to test, is to confirm a rumor that such device ignores the HDCP, the kind of copy protection that prevents video or audio from being played on "unauthorized" devices. But as far as I know, the output device needs to "play along". So I tried streaming a movie via a streaming box and I was happy to see that it plays without an issue.


Windows built-in App

Now it is time to carrying forward with my Time Shifting plan. My plan have a Plan A and a Plan B. Plan A is to come up with a functional Time Shift environment. But before then, my Plan B is to record the video and play it back. In the worst case, I can record the entire media and watch it later just like a plain old VCR. For that the Windows 10 Camera App almost made it. I wrote "almost" because it has a strange yet fatal drawback - as soon as the Camera App is minimized, the recording stops. It is puzzling why it is designed that way. The best I can do is to shift the window to the side and let it record in the background. But it is not ideal.

The other downside with the Camera App, is that it records in H.264 format. H.264 is the current standard in terms of compression and quality. However, the drawback is that the video cannot be played back until the authoring is complete. Which means, I cannot let the app record while opening the recording with another program such as VLC.

Swiss Army Knife of video player - VLC

VLC is like a swiss army knife of media player. It plays just about everything it throws at it. But it still cannot playback a partially written H.264 video. However, it comes with its own video capture and transcoding capability! So the first thing I want to consider is using VLC to time shift.

Surprisingly, VLC actually have a time shift functionality. But its definition of "time shift" is very different from mine. VLC's time shift allows it to pause a live stream and pick up where it left off. So far so good, but this is where the similarity ends. I can only unpause the video playback but not jump forward after a buffer is built up, which means I cannot skip commercials.

All is not lost, if VLC cannot do timeshift natively, perhaps I can use it to record and play it back? To do that, I need to use a functional known as "transcoding", which is to save the video stream into file using certain video/audio codec format. There are several online tutorials and it does work. However, there are quite a few quirks. The best transcoding is also H.264 variants which means I cannot playback during record. The VLC interface for recording from USB video tends to forget all my settings and also default to a very annoying resolution and fps. Also, I managed to record using MPEG2 format but the video is choppy and not good even at 720p. I tried using the "raw" option but to no avail. 

In the end, I still cannot get VLC to work the way I wanted. The trail went cold for a while and I put the project on hold.

Debut Video Recorder

And then, one day I decided to take another look and try to seek 3rd party video capture tool that can do what I want. I found a Windows 10 App called "Debut Video Recorder". This video recorder is available for free and it just happens to have a video format that it can record and played back mid-stream. That looks promising.



Bring in the Professional - FFMPEG

That is until I found a even better option. I came across a YouTube video about someone's quest of recording from the same USB HDMI capture stick using FFMPEG, he did a lot of leg work of making ffmpeg work with the stick. One of the key piece is the command to record video. In his example, he record using the regular H.264 format. However, I found out that ffmpeg actually supports a "copy" codec which does not do transcoding. This is perfect for my need.

I experimented with the options and found the perfect command for my need. The video format the USB stick output is motion jpeg, which is fine for playback. Using "copy" codec ensures the minimum impact to video quality and also reduce the CPU utilization. 

ffmpeg -f dshow -rtbufsize 100M -video_size 1280x720 -framerate 30 -vcodec mjpeg -i video="USB Video":audio="Digital Audio Interface (USB Digital Audio)" -vcodec copy -acodec copy vid_720p_30fps.avi

(Note: the video and audio device names may varies depending on other devices on your system)

Now, I have a video named vid_720p_30fps.avi that I have no problem playing back using VLC while the recording is in progress and skip forward as needed. Even better is the fact that this process uses very little CPU and I was able to perform this on a 15 years old Intel Core2Quad CPU with a regular HDD!



Video quality is 720p, likely due to limitation of the HDMI capture stick, but it is good enough for me.

So after over a year of acquiring the USB stick, I can finally enjoy time shifting HDMI video! Hopefully, on day I can realize my Plan A - having a time shifting player without the need of set up in advance.