gmgauthier.com/content/post/ffmpeg-scrapbook.md

56 lines
3.4 KiB
Markdown

---
title: "An ffmpeg scrapbook"
date: 2021-04-05T08:44:33+01:00
tags: ["ffmpeg", "media"]
topics: ["technology", "podcasting"]
draft: false
---
In order to produce videos, I have had to jump through a lot of hoops. One of those, is learning how to transcode video files with ffmpeg. This post is mostly a convenience for me. A place where I can dump copy-pasta command lines, so that I never forget them.
### Extracting video from YouTube
If you're initially uploading to YouTube (because its the only cellphone app that works well), and need to move the videos to other services that don't support syncing yet (or, their support is sketchy and broken), then use ffmpeg in cooperation with youtube-dl, and do this:
```
youtube-dl https://www.youtube.com/watch\?v\={video-id} --recode-video mp4 -x --ffmpeg-location /usr/local/lib/
```
Of course, you can recode it any way you like (presuming ffmpeg supports it). I use mp4 because it's universal. My videos are cellphone talking-head quality. So, I don't really need much more than that. The backslashes in the youtube url, are to accommodate the oddities of zshell.
### Remastering videos
If you need to re-render a video because Bitchute or Odysee are complaining about the video format, or you need to shrink it because your video service won't take large files (e.g. Minds and Locals), then do this:
```
ffmpeg -i {input-video}.mp4 -vcodec libx265 -crf 28 {output-video}.mp4
```
`libx256` is the open-source port of the the H.265 codec. `-crf 28` is the compression density of the video. The higher the number, the more loss there is. Most places tell you not to go any higher than 23 or 24, but frankly, the videos I do don't warrant Michael Bay levels of resolution, and the smaller I can get them, the easier they are to move to different platforms, and to store locally.
### Extracting audio from video
For converting video to audo, so I can post it on my podcast:
```
ffmpeg -i input.mp4 -vn -ar 44100 -ac 2 -ab 128 -f mp3 output.mp3
```
I use 128 because its a compromise value. You could do something smaller, if your audio is only spoken word. I've seen it as low as 64, and for that use, probably no more than 192 is really necessary. But for music or other high quality sound, you probably want 256 or 320.
### Bonus: getting thumbnails
Bonus: For those of you who prefer to make your own thumbnails (rather than just accepting whatever Youtube or Rumble insist on you using), here's a trick for grabbing frames from your own video, without having to fiddle with screenshot utilities:
```
ffmpeg -i input.mp4 -r {frame capture rate} -f image2 -s {[width]X[height]} {filename template}.png
```
The `-r` frame cature rate is basically just how many images you want per second. It defaults to 25. Probably somewhere between 1 and 3 is more than enough. The `-f` option just forces the output to images, rather than some video format like `vcd` or `alsa`. The `-s` option will set the width and height of the images. The filename template follows standard unix/python/c string formatting rules. I've seen examples like `filename-%3d.png` floating around. `%3d` just means three zero-padded decimal integers. So, your filenames will look like, "`filename-001.png`, `filename-002.png`, `filename-003.png`", and so forth.
### Where else to look
For more fancy footwork, you can look here:
* https://ostechnix.com/20-ffmpeg-commands-beginners/
* https://ffmpeg.org/ffmpeg.html