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.

Thursday, November 11, 2021

Sink stopper hack

Once in a while, I need to unclog my kids' bathroom sink because it is clog up with toothpaste. It isn't hard to do. Just remove the sink stopper and clean it. But the stopper is locked by the pivot rod so I need to unscrew the pivot nut to unlock the stopper. Therefore, I also need to remove the stuffs inside the sink vanity to prevent them from getting dirty.


It would be much better if I don't need to remove the pivot rod under the sink but instead remove the stopper from top. 

I come up with a simple trick that I use which I never saw elsewhere. 

The stopper has a loop which allows the pivot rod to pass through so it can pop the stopper after it is depressed. By cutting the loop, I can remove the stopper by rotating it 90 degrees and pull up. See the pictures below:

simple plastic cutter will do
(Notice the rod inside and the space on its side. So there is room to insert and rotate.)

After the mod, the stopper change from a loop into a hook. I like to inside the stopper in a way that the hook is toward the back of the sink so it will not come loose easily. The stopper can still be popped by the lift rod handle and the stopper does not come loose unless I rotate it 90 degrees.

Have Fun!


Thursday, December 10, 2020

Poor man's Document camera

I needed a document camera for presentation. Typical camera cost about $100 on Amazon:

https://www.amazon.com/INSWAN-Document-Camera-Auto-Focus-Supplemental/dp/B07PQJZK66

That is a bit too much for occasional uses. So I decided to make my own.



It is made with an old webcam that I have around and an old desk lamp that I bought for $20. 


Held in place by rubber band

A new webcam can be bought for about $30 but I particularly like my old Logitech C270 because it is fixed focused. A small problem is that the webcam is factory calibrated to focus from about 18" (a typical distance for web chat) to infinity. Unfortunately, 18" is too far for presentation because a typical drawing on paper will be too small. Fortunately, there is a simple fix for that.

Changing it, however, means that the webcam will not focus to infinity and will start becoming bluring about 3 ft or so. To me, it is an acceptable trade off, and it is reversible. Not that I really need the old webcam.

It should be noted that the webcam must not be too heavy. As the lamp is not design to withstand weight, especially when it is attached to the tip.

Also, the webcam is supported across multiple platforms. I have tried MacOS, Windows and ChromeOS. They all worked. Software-wise, no issues with Zoom, WebEx and Google Meet. The 2 former allows me to switch between built-in webcam and external webcam with ease. Google Meet can be done via its settings menu.




The reason for choosing the lamp is that it is adjustable and also, surprise, surprise, has a light function. So it helps illuminate the writing/drawing.

I thought of making it such that I can flip the webcam so that I use it for conference. But the problem is that the video would be upside down. Some application like Zoom let me flip the video but it is too cumbersome. 

That's it for now!


Tuesday, April 7, 2015

Lunar eclipse video without star tracker

Last Saturday was a (near) total lunar eclipse. I planned for taking a series of time-lapse photos to make a video of the vanishing moon. 

The weather is cooperating and the time-lapse was done successfully. I ended up with a decent amount of pictures. To make a video, I used my favorite free software - Windows Movie Maker.





Making video is easy. I pre-cut photos into 16:9 and just import all the pictures and change the Duration to 0.1s under the Edit tab. But the resulting video is bad. The moon appears to be jumping across the screen (need to view in full screen to see).



I realized that the moon is traveling faster than I expected and my time-lapse interval of 15s is way too long.

I wish my camera can turn along with the moon. But this is not possible after the fact. And a decent star tracker costs hundreds of dollars. Not something I am willing to invest in.

Time for some damage control. I realize I am anyway throwing away resolution when down-sampling pictures to 1080p. So why not use the full resolution and crop the picture such that the moon appears to be moving slower?

In order to do that, I must not be cropping constantly the same offsets as I need to account for the movement of the moon and "slow it down" (Essentially "zoom and pan"). Typical batch cropping tools like IrfanView isn't up for the task. I will need a command line based utility.

If it was back in the college days, I would go with netpbm but things have changed. After a bit of googling, I decided on ImageMagick. The syntax is fairly straight forward:

convert {width}x{height}+{x}+{y} {old file} {new file}


I decided 800x480 is the size I would use. So I just have to experiment with the x, y offsets. I decided to generate a script using my language of choice:

main()
{
    int x = 330;
    int y = 390;
    int p;

    for (p = 9021; p <= 9219; p++) {
        printf("convert -crop 800x480+%d+%d ../DSC0%d.jpg %d.jpg\n",
            x, y, p, p);
        x += 16;
        y += 12;
    }
}


Here is the final video. I guess I am happy with the effect: