A Script for Resumable Lecture Videos
Friday, May 20th 2016
During Autumn quarter this year, all of my class lectures were recorded and available online. I initially tried to attend every lecture, but the ability to skip class and watch it later was too tempting. I soon found myself waking up at 2PM everyday and watching all my lectures through the SCPD video system.
This lifestyle presented couple of problems. First, the SCPD player was annoying to use since bad internet connections would force me to reload and lose my place in the video. Second, I found myself rebooting frequently to switch operating systems – this would also cause me to lose my place. I could use the download feature on the SCPD site to solve the first problem, but the second still remained.
Instead of fixing my life, I decided to fix my technology. What I really needed was a video player that could remember where I left off. This would also allow me to split my lectures into parts and rotate between them (I found that this prevents me from getting bored while watching 2-hr lectures). I looked at a wide variety of video players, but I didn’t find any ideal solutions. Most players don’t remember positions, and those that do typically only remember positions on the last video viewed. Furthermore, I often switch OS’s (primarily OSX and Linux), and I want my timecodes to work on both (and ideally be synchronized). Given these constraints, I figured the only solution was to make my own tool.
I obviously didn’t create a video player from scratch – instead I used MPlayer, a GUI-less video player that is launched from the command-line. What makes MPlayer awesome is that it outputs video progress to stdout as the video plays. Using this feature, I was able to write a simple wrapper script to launch MPlayer and read/store the timecodes.
MPlayer is interesting since, as the video progresses, the application actually erases the previous line in order to write the new timecode. It does so using “\r” (carriage return) to return to the beginning of the line and rewrite the previous data. From there, it was as simple as splitting the stdout stream on that code and extracting the video position with a regex. The timecode is saved every five seconds. When the same video is opened again, the wrapper script uses the “-ss” flag to pass in the saved timecode.
In order to share timecodes between machines, the timecodes are stored in a folder in my home directory, indexed by the SHA-1 hash of the video file’s contents. I then set up that folder to be a symlink into my Dropbox and voila! Cross-platform synchronized video resuming in less than 100 lines of code, which you can download and use here.
This simple project shows how existing tools can be recombined to solve a daily problem and boost productivity. I’ve encountered tons of these little hacks (every engineer has a couple!) and I’m always blown away by the cool ideas that people think up. Leave a comment if you have any similar projects you’d like to share!#workflow