lk/vision/ffmpeg.md

167 lines
2.9 KiB
Markdown
Raw Normal View History

2022-06-18 13:48:28 +00:00
---
title: "ffmpeg"
tags: [ "Documentation", "Sound", "Vision" ]
---
2021-12-20 15:57:16 +00:00
# Basics
2022-06-18 13:48:28 +00:00
Translate a media file to a new type.
2021-12-20 15:57:16 +00:00
ffmpeg -i [input file] output_file.mkv
The input file might be a device, such as a camera.
#Record screen
2022-06-18 13:48:28 +00:00
2021-12-20 15:57:16 +00:00
Take the format as 'grab the x11 screen'.
```bash
ffmpeg -f x11grab -s [screensize] -i :0.0 out.mkv
```
2021-12-20 15:57:16 +00:00
Get screensize with
```bash
xrandr -q
```
2021-12-20 15:57:16 +00:00
or maybe just...
```bash
ffmpeg -f x11grab -s "$(xdpyinfo | grep dimensions | awk '{print $2}')" -i :1.0 out.mkv
```
2021-12-20 15:57:16 +00:00
#Add default pulse audio
```bash
ffmpeg -f x11grab -s [screensize] -i :0.0 -f alsa -i default out.mkv
```
2021-12-20 15:57:16 +00:00
For problems, see pavucontrol.
# Random online suggestion
ffmpeg -video_size "$(xdpyinfo | grep dimensions | awk '{print $2}')" -f x11grab -i :0.0 -f pulse -ac 2 -i default ~/out.mkv
# Rotate
```bash
ffmpeg -i in.mov -vf "transpose=1" out.mov
```
2021-12-20 15:57:16 +00:00
0 = 90 Counterclockwise and verfical flip (default)
1 = 90 Clockwise
2 = 90 CounterClockwise
3 = 90Clockwise and vertical flip
# Lower Video Quality
2022-06-18 13:48:28 +00:00
2021-12-20 15:57:16 +00:00
A crf quality of 18 is high, while 24 is low quality.
ffmpeg -i input.mp4 -vcodec libx264 -crf 20 output.mp4
# convert
Check for supported formats:
```bash
ffmpeg -formats
```
2021-12-20 15:57:16 +00:00
To convert from mkv to mp4 we can use a codec rather than proper conversion. Both are wrappers around other formats, so this conversion loses less quality than other conversion types.
```bash
ffmpeg -i LostInTranslation.mkv -codec copy LostInTranslation.mp4
```
2021-12-20 15:57:16 +00:00
Opus to mp3
```bash
ffmpeg -i song.opus song.mp3
```
2021-12-20 15:57:16 +00:00
```bash
ffmpeg -i video.flv video.mpeg
```
2021-12-20 15:57:16 +00:00
```bash
ffmpeg -i input.webm -qscale 0 output.mp4
```
2021-12-20 15:57:16 +00:00
# Video to Audio
```bash
ffmpeg -i input.mp4 -vn output.mp3
```
2021-12-20 15:57:16 +00:00
# Convert all mkv files to mp4
```bash
for i in *.mkv; do
```
2021-12-20 15:57:16 +00:00
> ffmpeg -i "$i" -codec copy "${i%.*}.mp4"
```bash
done
```
2021-12-20 15:57:16 +00:00
# Change resolution
```bash
ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4
```
2021-12-20 15:57:16 +00:00
Or just crop:
```bash
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
```
2021-12-20 15:57:16 +00:00
Or aspect ratio:
```bash
ffmpeg -i input.mp4 -aspect 16:9 output.mp4
```
2021-12-20 15:57:16 +00:00
Or trim to start and stop times:
```bash
ffmpeg -i input.mp4 -ss 00:00:50 -codec copy -t 50 output.mp4
```
2021-12-20 15:57:16 +00:00
Indicate start times with -ss and time with -t in seconds.
Or split a video into parts:
```bash
ffmpeg -i input.mp4 -t 00:00:30 -c copy part1.mp4 -ss 00:00:30 -codec copy part2.mp4
```
2021-12-20 15:57:16 +00:00
# Compress Video
```bash
ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4
```
2021-12-20 15:57:16 +00:00
# Extract Images from Video
-r sets the frame rate, and -f selects the format.
```bash
ffmpeg -i input.mp4 -r 1 -f image2 image-%2d.png
2021-12-20 15:57:16 +00:00
```
# Add Images to Audio
2021-12-20 15:57:16 +00:00
```bash
$ ffmpeg -loop 1 -i inputimage.jpg -i inputaudio.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4
2021-12-20 15:57:16 +00:00
```
# Add Subtitles
```bash
fmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mp4
```
2021-12-20 15:57:16 +00:00