I watch a ton of animu. I also take a lot of screenshots while doing so. Occasionally, I will post some of them – mostly the silly or cute ones – to my Twitter Account, sometimes with a more or less funny comment.
Last night. I had the idea of making a separate account so I could spam screenshots as much as I want without annoying my followers too much. I immediately secured @slowpokeshots, then realized it would be pretty cool if I could automate this. Since I had planned to write something interfacing with the Twitter API for quite a while, I quickly looked for some viable libraries, found this nice python package, and set out to hack something together.
Now, some two hours later, I have a working, if somewhat hackish, script that automagically uploads every screenshot I take in mpv (the player you should seriously be using if you’re on any Linux distro) to the aforementioned Twitter account.
Now, how does this work?
The simplest part is actually interfacing and uploading stuff to Twitter. Basically everything is already taken care of by the aforementioned python library, though I had a little derpage with OAuth in the beginning.
The somewhat harder part was interfacing with mpv, which has no API in the strictest sense. What it does have, however, is the ability to read commands from a named pipe (a FIFO file). I’ve fucked around with this a bit before because it essentially allows you to very simply remote control mpv over SSH – you simply echo stuff like “pause” into the file, MPV(1) has a list of all
commands it accepts (see the “List of Input Commands” section).
This input file must either be passed to mpv with every invocation via –input-file, or configured in ~/.mpv/config. In my case, it’s set to /tmp/mpv.fifo, which is created during X startup.
Now, there are three commands for mpv of interest: screenshot, screenshot_to_file, and run.
The first two are somewhat self-explaining. screenshot takes a shot, named according to whatever is specified in the screenshot-template option. In my case, it will put them into ~/screenshots and name it after the filename of the video plus a four digit incrementing number (again, see MPV(1) for how to configure screenshot-template). This is not super-important to the use case at hand, but I thought I’d mention it.
screenshot_to_file, as the name implies, takes a filename as an argument, where it will save the taken screenshot. Both screenshot and screenshot_to_file take an optional argument (first argument for the former, second for the latter) which decides whether subtitles are included in the screenshot (“subtitles”, the default if nothing is given) or not (“video”). Per default, screenshot subtitles is bound to s in mpv, and screenshot video to S. I use both quite a lot.
run is for running shell commands, nothing much more to explain about it.
Now, what my script does is the following: when called, it will use the aforementioned named pipe to tell mpv to take a screenshot to a specified location with screenshot_to_file, then another regular screenshot (because I want my shots archived), waits for the first shot to appear, and uploads it to Twitter. I’ve bound this script, using the run command, to the keys formerly used for taking screenshots – the whole process is transparent.
I’ve posted the code to a gist because I don’t think I’m gonna change anything about it in the future (and if I do, I can still put it in a repository proper).
It’s public domain. Use it if you like it (you might want to change the API keys and make your own version, though).