http://blog.azuresky.ca/ azuresky.ca 2019-04-05T21:57:00Z Michael Chang http://azuresky.ca/ tag:blog.azuresky.ca,2019-04-05:/blog/2019/04/libnvidia-encode1/ libnvidia-encode1 2019-04-05T21:57:00Z 2019-04-05T21:57:00Z <h2 id="how-it-all-started">How it all started…</h2> <blockquote> <p>“I just want to record my screen.”</p> <p>“Oh, you can use ffmpeg to do that.”</p> <p>(the next day)</p> <p>“My recording is flickering. Is it supposed to do that?”</p> <p>“Oh, hmm…”</p> </blockquote> <h2 id="prerequisites">Prerequisites</h2> <ul> <li>You have a Debian Linux computer with a supported NVidia GPU.</li> <li>You have the proprietary NVidia drivers installed.</li> </ul> <h2 id="hardware-accelerated-screen-recording-with-ffmpeg">Hardware-accelerated screen recording with ffmpeg</h2> <p><strong>tl;dr:</strong> Try installing <code>libnvidia-encode1</code>.</p> <p>From a Google search, the canonical article on the ffmpeg wiki appears to be here: <a href="https://trac.ffmpeg.org/wiki/Capture/Desktop">https://trac.ffmpeg.org/wiki/Capture/Desktop</a></p> <p>It include the following example:</p> <pre><code>$ ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 output.mp4 </code></pre> <p>Let’s try a minimal case:</p> <pre><code>$ ffmpeg -f x11grab -i :0.0 output.mp4 ... s speed=0.59x </code></pre> <p>Hmm, just outputting a 640x480 video with software encoding is not consistently real-time on this computer, when using software encoding.</p> <p>But I know this computer has a recent NVidia card and the proprietary driver, so maybe we can use hardware encoding?</p> <pre><code>$ ffmpeg -encoders ... V..... h264_nvenc NVIDIA NVENC H.264 encoder (codec h264) ... </code></pre> <p>Okay, looks like this <code>ffmpeg</code> was built with <code>h264_nvenc</code>. Let’s use it:</p> <p><img src="/images/2019/libnvidia-encode-error.png" alt="A screenshot of the following shell snippet."></p> <pre><code>$ ffmpeg -f x11grab -i :0.0 -c:v h264_nvenc output.mp4 ... [h264_nvenc @ 0x55a55bb9c360] The minimum required Nvidia driver for nvenc is 378.13 or newer Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height Conversion failed! </code></pre> <p>Huh? I’m pretty sure a more recent driver is installed. I have 390.87, and I’m pretty sure that’s newer than 378.13.</p> <p><img src="/images/2019/nvidia-version-390-87.png" alt="A screenshot of the NVidia X Server Settings app."></p> <p>Let’s look at that error message again…</p> <pre><code>Cannot load libnvidia-encode.so.1 [h264_nvenc @ 0x55a55bb9c360] The minimum required Nvidia driver for nvenc is 378.13 or newer Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height Conversion failed! </code></pre> <p><code>Failed to load libnvidia-encode.so.1</code>? What package provides that? Turns out it’s a separate package called <code>libnvidia-encode1</code>:</p> <pre><code>$ sudo apt-get install libnvidia-encode1 </code></pre> <p>And now our screen recording works:</p> <pre><code>$ ffmpeg -video_size 2560x1600 -f x11grab -i :0.0 -c:v h264_nvenc output.mp4 </code></pre> <h2 id="hardware-accelerated-screen-recording-with-obs">Hardware-accelerated screen recording with OBS</h2> <p>Install OBS, and <code>libnvidia-encode1</code>:</p> <pre><code>$ sudo apt-get install libnvidia-encode1 obs-studio </code></pre> <h3 id="configuring-obs-to-use-nvenc">Configuring OBS to use NVENC</h3> <p>Hardware (NVENC) should just show up in the list of Encoders after installing libnvidia-encode1. (You may need to restart Open Broadcaster Software.)</p> <p>If you don’t get the first-run wizard, delete <code>${HOME}/.config/obs-studio/</code>. (Obviously, make a backup before you go deleting your OBS configuration…)</p> <ol> <li>Select <em>optimize just for recording</em>. (Unless you plan to stream, in which case you might want the extra dialogs for setting up the service you want to stream to.) Click <em>Next</em>.</li> <li>Pick some video settings. Click <em>Next</em>.</li> <li>Verify <em>Recording Encoder: Hardware (NVENC)</em>. If it says Software, or tries to run an encoding benchmark, double-check that you installed libnvidia-encode1 and obs-studio. Click <em>Apply Settings</em>.</li> <li>Under Sources, click <em>+</em>, then <em>Screen Capture (XSHM)</em>.</li> <li>In <em>Create/Select Source</em> dialog, click <em>OK</em>. In the <em>Properties for ‘Screen Capture (XSHM)’</em> dialog, click <em>OK</em>. Your screen should now show in the preview area.</li> </ol> <h3 id="recording-only-part-of-the-screen-with-obs">Recording only part of the screen with OBS</h3> <ol> <li>Right-click the preview of the source you configured. Select <em>Transform &gt; Edit Transform…</em> </li> <li>Specify the <em>Crop</em> in the <em>Scene Item Transform</em> dialog. Click <em>Close</em>.</li> <li>Click the <em>Settings</em> button (or <em>File &gt; Settings</em>).</li> <li>Under <em>Video</em>, modify <em>Base Resolution</em> and <em>Output Resolution</em> as needed. You can type a custom resolution into the drop-down, like <code>1280x800</code>, for both. Click <em>OK</em>.</li> </ol> <h3 id="making-a-recording-with-obs">Making a recording with OBS.</h3> <ol> <li>Verify your screen capture source is selected.</li> <li>Click <em>Start Recording</em>. (You can also set a hotkey for this in Settings.)</li> <li>Perform the actions you want to record.</li> <li>Click <em>Stop Recording</em>. (You can also set a hotkey for this.)</li> <li>Recorded file will be created in the directory you set under <em>Settings &gt; Output &gt; Recording Path</em>. (By default, this is your home directory.)</li> </ol> tag:blog.azuresky.ca,2018-06-19:/blog/2018/06/peach-salad/ Peach Salad 2018-06-19T07:31:50Z 2018-06-19T07:31:50Z <p>Have you ever tried to define what is and isn’t a salad? The Wikipedia article basically comes down to “a mixture, usually vegetables, typically chilled, flavored with salad dressing, with notable exceptions”. You might as well say, “vegetables, except when it’s not”.</p> <p>After puzzling over this for a bit, I’ve found a definition I like better: a salad is <em>a mixture of things someone likes to eat</em>.</p> <p><strong>The true power of salad is realizing that someone could be you!</strong></p> <p>This salad is a recipe from my mom. The first time I saw this was when I was a kid. My mom had made it for herself, and still I remember being pleasantly surprised by how good it was when she let me try some of hers.</p> <p>I find that the contrast between the flavor of the fresh fruit and the dried plum makes everything taste better. Salt also draws out the water in fresh fruit, concentrating their flavour.</p> <p>I always do this recipe “by eye”, so you may need to adjust the amount of salt. But nectarines and tomatoes also come in different shapes and sizes, so, whatever. Add two or three if you like! The recipe also works with other kinds of stone fruit (such as peaches and fresh plums), either as a substitute for nectarines or in addition to them.</p> <h2 id="ingredients">Ingredients</h2> <ul> <li>about 1 Tomato</li> <li>about 1 Nectarine</li> <li>approximately 1 teaspoon salt</li> <li>approximately 3 Chinese dried salted plums (usually brown or red, and sold in clear bags at the Chinese grocery store, with or without pits)</li> </ul> <h2 id="utensils">Utensils</h2> <ul> <li>cutting board</li> <li>knife (preferably a paring knife, but a plastic knife will do in a pinch)</li> <li>two bowls</li> <li>a fork or toothpicks</li> </ul> <h2 id="directions">Directions</h2> <ol> <li>Coarsely chop the tomato and nectarine into a bowl. Discard the nectarine pit.</li> <li>Sprinkle the salt over the chopped tomato and nectarine.</li> <li>Tear the chinese plums into smaller pieces (discard the pits, or use them to make plum tea) and also sprinkle over the tomato and nectarine.</li> <li>Toss to combine. (I like to use the second bowl as a lid when tossing to combine.) Enjoy with a fork or toothpicks.</li> </ol> tag:blog.azuresky.ca,2018-06-19:/blog/2018/06/picky-eater/ Picky Eater 2018-06-19T07:17:40Z 2018-06-19T07:17:40Z <p>I would say that I’m a very picky eater. I like eating peaches, but I can’t stand peach juice. Apple slices are fine, but I’ll pass on a whole apple. Cheesecake is good, and cheesecake with chocolate drizzled is even better. But I’ll skip on chocolate cheesecake and chocolate chip cheesecake. “What is your favourite food?” was one of my least favorite questions as a kid. I know enough to know that neither you nor I actually want to spend half an hour talking about what I want in my lunch box.</p> <p>When I was in elementary school, my mom would sometimes pack a whole, raw tomato in my lunchbox. I hated getting teased by my classmates for it, and I wondered why my mom didn’t just pack me a Lunchables like everyone else.</p> <p>With hindsight, I now know that – like many parents of kids with Asperger’s – my mom would stress over what to feed me, to make sure I ate enough. I know now that she packed those tomatoes because she knew I would eat them. And that’s one of the many ways she showed me that she loved me – by remembering what I would and wouldn’t eat, and planning my school lunches around that.</p> <p>Thanks, Mom.</p> tag:blog.azuresky.ca,2014-03-11:/blog/2014/03/how-do-you-even-lose-data-when-using-git/ How do you even lose data when using git? 2014-03-11T06:09:00Z 2014-03-11T06:09:00Z <p><em>I originally wrote this article for Volume 124, Issue 5 of mathNEWS.</em></p> <p>It’s 18:00 on a Saturday night, and you see your friend Joseph force push to your git repo for compilers. Six minutes later, a commit is added to that repo: “Joseph is an idiot.” What on earth happened, and how can you prevent this catastrophe from afflicting your group?</p> <p>Root cause analysis determined the cause of this particular disaster to be “Untracked files”. How? <code>git status</code> lists files that haven’t been committed to the repository under the heading “Untracked files”. However, if having “Untracked files” is the normal state for your repository, then you’ll have to manually sift through the output of <code>git status</code> to realize you forget to commit a file you actually want to keep around for later. The chances of you doing that for each commit you make the night before the deadline? Nil. So you’ll lose the files once you <code>git checkout</code> another branch… and once you realize, it’s too late, and you’ll have to recreate the files from memory. The loss of precious minutes when you least have them to spare!</p> <p>How can you protect you code and your loved ones from this disaster? It’s easy! You simply need to make judicious use of <code>.gitignore</code>. Create a file named <code>.gitignore</code> in your repository and commit it; each line that doesn’t start with a <code>#</code> contains a pattern matching files that git shouldn’t track or add to the repository. Chances are, you won’t even have to write the file yourself – you can probably concatenate together the files for your two or three favourite programming languages and editors (say, Go.gitignore, Erlang.gitignore, and Global/Kate.gitignore) from the collection at https://github.com/github/gitignore.</p> <p>“But I use LaTeX,” you might protest, “and I also need to commit <code>.log</code> files generated by my code!” I do agree, that <a href="https://github.com/github/gitignore/blob/master/TeX.gitignore"><code>TeX.gitignore</code></a> contains some rather sweeping patterns, such as <code>*.log</code> and <code>*.out</code>. There’s a simple pattern to handle this too: I create a directory named <code>latex</code> or <code>report</code> in the top of my repository, place my <code>.tex</code> files in it, as well as a copy of <code>TeX.gitignore</code> named <code>.gitignore</code>. Since the rules in <code>latex/.gitignore</code> only affect files stored under <code>latex/</code>, you’re free to add <code>.log</code> and <code>.out</code> files anywhere else in the tree.</p> <p>In fact, I’ve got a bit of a pattern going now with how I start my assignments:</p> <ul> <li>se465 (folder so I can tell each "a1" repo apart) <ul> <li>a1 (this is a git repo) <ul> <li>.gitignore</li> <li>latex <ul> <li>.gitignore</li> <li>Makefile</li> <li>a1_sub.tex</li> </ul> </li> <li>q1 <ul> <li>q1.java</li> </ul> </li> <li>...</li> </ul> </li> </ul> </li> </ul> <p>Now if only I had figured this out in first year. But now that I have, you don’t have to!</p> <p>Just take a little bit of time when you start your next CS assignment, and make sure git tells you about only the Untracked files you care about!</p> <p>!able</p> tag:blog.azuresky.ca,2014-02-12:/blog/2014/02/you-keep-me-up-at-night/ You Keep Me Up At Night 2014-02-12T01:41:00Z 2014-02-12T01:41:00Z <p><em>I originally wrote this article for Volume 124, Issue 3 of mathNEWS.</em></p> <p>A fortnight ago, I was transported to the far-off land of Oshawa to engage in a mimicry of our government’s system of discussing of questions of public interest. We spoke of drivers’ education, and of automatic transmissions.</p> <p>Ever since we got back, there’s only been one thing that’s kept me up at night:</p> <p>The way your glasses fall squarely on your nose,</p> <p>The way the gel holds your combed-over hair in place,</p> <p>The way your knuckles look when you hold a pen in just that way,</p> <p>The square shoulders of your suit,</p> <p>The crisp folds in your tie,</p> <p>The way you smiled.</p> <p>“The Member of the Opposition is putting up a smokescreen, instead of focusing on the issues at hand,” you would say, but all I can focus on is you.</p> <p>Dear Prime Minister, hailing from from the University of Toronto, congratulations on Second Place. You’ll always be first in my heart.</p> <p>Happy Valentine’s Day!</p> <p>!atraitoriswear</p>