Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active August 29, 2015 14:00
Show Gist options
  • Save hightemp/11067566 to your computer and use it in GitHub Desktop.
Save hightemp/11067566 to your computer and use it in GitHub Desktop.

###1. Получение информации о видеофайле

ffmpeg -i video.avi

###2. Превратить набор картинок в видео

ffmpeg -f image2 -i image%d.jpg video.mpg

Эта команда преобразует все картинки из текущей директории (названные image1.jpg, image2.jpg и т.д.) в видеофайл ideo.mpg

(примечание переводчика: мне больше нравится такой формат:

ffmpeg -r 12 -y -i "image_%010d.png" output.mpg

здесь задаётся frame rate (12) для видео, формат «image_%010d.png» означает, что картинки будут искаться в виде image_0000000001.png, image_0000000002.png и тд, то есть, в формате printf)

###3. Порезать видео на картинки

ffmpeg -i video.mpg image%d.jpg

Эта команда создаст файлы image1.jpg, image2.jpg и т.д., поддерживаются так же форматы PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TIFF, SGI.

###4. Конвертировать видео в формат iPod/iPhone

ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4

Пояснения:

Источник: source_video.avi
Аудио кодек: aac
Битрейт аудио: 128kb/s
Видео кодек: mpeg4
Битрейт видео: 1200kb/s
Размер видео: 320 на 180 пикселей
Полученное видео: final_video.mp4

###5. Конвертировать видео в формат PSP

ffmpeg -i source_video.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac final_video.mp4

Пояснения:

Источник: source_video.avi
Аудио кодек: aac
Битрейт аудио: 32kb/s
Видео кодек: xvid
Битрейт видео: 1200kb/s
Размер видео: 320 на 180 пикселей
Полученное видео: final_video.mp4

###6. Извлечь звуковую дорожку из видео и сохранить в mp3

ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3

Пояснения:

Источник: source_video.avi
Битрейт аудио: 192kb/s
Выходной формат: mp3
Полученный аудиофайл: sound.mp3

###7. Конвертировать wav в mp3

ffmpeg -i son_origine.wav -vn -ar 44100 -ac 2 -ab 192 -f mp3 son_final.mp3

###8. Конвертировать .avi в .mpg

ffmpeg -i video_origine.avi video_finale.mpg

###9. Конвертировать .mpg в .avi

ffmpeg -i video_origine.mpg video_finale.avi

###10. Конвертировать .avi в .gif (без сжатия)

ffmpeg -i video_origine.avi gif_anime.gif

###11. Добавить звук к видео

ffmpeg -i son.wav -i video_origine.avi video_finale.mpg

###12. Конвертировать .avi в .flv

ffmpeg -i video_origine.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv video_finale.flv

###13. Конвертировать .avi в .dv

ffmpeg -i video_origine.avi -s pal -r pal -aspect 4:3 -ar 48000 -ac 2 video_finale.dv

или

ffmpeg -i video_origine.avi -target pal-dv video_finale.dv

###14. Конвертировать .avi в mpeg для DVD-плееров

ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 finale_video.mpeg

Пояснения:

Выходной формат: pal-dvd
Максимальный размер для выходного файла: 2000000000 (2 Gb)
Широкоэкранный формат: 16:9

###15. Сжать .avi в DivX

ffmpeg -i video_origine.avi -s 320x240 -vcodec msmpeg4v2 video_finale.avi

###16. Сжать OGG Theora в mpeg DVD

ffmpeg -i film_sortie_cinelerra.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 film_termin.mpg

###17. Сжать .avi в SVCD mpeg2

Формат NTSC:

ffmpeg -i video_origine.avi -target ntsc-svcd video_finale.mpg

Формат PAL:

ffmpeg -i video_origine.avi -target pal-svcd video_finale.mpg

###18. Сжать .avi в VCD mpeg2

Формат NTSC:

ffmpeg -i video_origine.avi -target ntsc-vcd video_finale.mpg

Формат PAL:

ffmpeg -i video_origine.avi -target pal-vcd video_finale.mpg

###19. Многопроходное кодирование с помощью ffmpeg

ffmpeg -i fichierentree -pass 2 -passlogfile ffmpeg2pass fichiersortie-2

###20. Удалить все звуковые дорожки

ffmpeg -i input_file.mp4 -vcodec copy -an output_file.mp4

###21. Вырезать часть видео и сохранить

ffmpeg -i input -ss 00:01:10.00 -t 00:00:40.00 -c copy output 

###22. Создать видео из изображения

ffmpeg -y -loop 1 -i input.jpg -s 540x360 -vf fps=30 -pix_fmt yuv420p -c:v libx264 -t 1.00 output.mp4

###Screen cast

FFmpeg includes the x11grab and ALSA virtual devices that enable capturing the entire user display and audio input.

First create test.mkv with lossless encoding:

ffmpeg -f x11grab -video_size 1920x1080 -i $DISPLAY -f alsa -i default -c:v ffvhuff -c:a flac test.mkv

where -video_size specifies the size of the area to capture. Check the FFmpeg manual for examples of how to change the screen or position of the capture area. Then you may process the MKV into a smaller WebM file:

ffmpeg -i test.mkv  -c:v libvpx -c:a libvorbis  -b:v 2000k -q:a 3 test.webm

###Recording webcam

FFmpeg supports grabbing input from Video4Linux2 devices. The following command will record a video from the webcam, assuming that the webcam is correctly recognized under /dev/video0:

ffmpeg -f v4l2 -s 640x480 -i /dev/video0 output.mpg

The above produces a silent video. It is also possible to include audio sources from a microphone. The following command will include a stream from the default ALSA recording device into the video:

ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 output.mpg

To use PulseAudio with an ALSA backend:

ffmpeg -f alsa -i pulse -f v4l2 -s 640x480 -i /dev/video0 output.mpg

For a higher quality capture, try encoding the output using higher quality codecs:

ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 -acodec flac -vcodec libx264 output.mkv

###VOB to any container

Concatenate the desired VOB files into a single stream and mux them to MPEG-2:

cat f0.VOB f1.VOB f2.VOB | ffmpeg -i - out.mp2

###x264 lossless

The ultrafast preset will provide the fastest encoding and is useful for quick capturing (such as screencasting):

ffmpeg -i input -vcodec libx264 -preset ultrafast -qp 0 -acodec copy output.mkv

On the opposite end of the preset spectrum is veryslow and will encode slower than ultrafast but provide a smaller output file size:

ffmpeg -i input -vcodec libx264 -preset veryslow -qp 0 -acodec copy output.mkv

Both examples will provide the same quality output.

###x265

In encoding x265 files, you need to specify the aspect ratio of the file via -aspect width:height. Example :

ffmpeg -i test.mp4 -c:v hevc -aspect 1920:1080 test_hevc.mp4

###Single-pass MPEG-2 (near lossless)

Allow FFmpeg to automatically set DVD standardized parameters. Encode to DVD MPEG-2 at ~30 FPS:

ffmpeg -i video.VOB -target ntsc-dvd -q:a 0 -q:v 0 output.mpg

Encode to DVD MPEG-2 at ~24 FPS:

ffmpeg -i video.VOB -target film-dvd -q:a 0 -q:v 0 output.mpg

###x264: constant rate factor

Used when you want a specific quality output. General usage is to use the highest -crf value that still provides an acceptable quality. Lower values are higher quality; 0 is lossless, 18 is visually lossless, and 23 is the default value. A sane range is between 18 and 28. Use the slowest -preset you have patience for. See the x264 Encoding Guide for more information.

ffmpeg -i video -vcodec libx264 -preset slow -crf 22 -acodec libmp3lame -aq 4 output.mkv

-tune option can be used to match the type and content of the of media being encoded.

###YouTube

FFmpeg is very useful to encode videos and strip their size before you upload them on YouTube. The following single line of code takes an input file and outputs a mkv container.

ffmpeg -i video -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy output.mkv

For more information see the forums. You can also create a custom alias ytconvert which takes the name of the input file as first argument and the name of the .mkv container as second argument. To do so add the following to your ~/.bashrc:

youtubeConvert(){
        ffmpeg -i $1 -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy $2.mkv
}
alias ytconvert=youtubeConvert

See also Arch Linux forum thread.

###Two-pass x264 (very high-quality)

Audio deactivated as only video statistics are recorded during the first of multiple pass runs:

ffmpeg -i video.VOB -an -vcodec libx264 -pass 1  -preset veryslow \
-threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 -f rawvideo -y /dev/null

Container format is automatically detected and muxed into from the output file extenstion (.mkv):

ffmpeg -i video.VOB -acodec libvo-aacenc -ab 256k -ar 96000 -vcodec libx264 \
-pass 2 -preset veryslow -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 video.mkv

Tip: If you receive Unknown encoder 'libvo-aacenc' error (given the fact that your ffmpeg is compiled with libvo-aacenc enabled), you may want to try -acodec libvo_aacenc, an underscore instead of hyphen.

###Two-pass MPEG-4 (very high-quality)

Audio deactivated as only video statistics are logged during the first of multiple pass runs:

ffmpeg -i video.VOB -an -vcodec mpeg4 -pass 1 -mbd 2 -trellis 2 -flags +cbp+mv0 \
-pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 -b 3000k \
-f rawvideo -y /dev/null

Container format is automatically detected and muxed into from the output file extenstion (.avi):

ffmpeg -i video.VOB -acodec copy -vcodec mpeg4 -vtag DX50 -pass 2 -mbd 2 -trellis 2 \
-flags +cbp+mv0 -pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 \
-b 3000k video.avi
Introducing threads=n>1 for -vcodec mpeg4 may skew the effects of motion estimation and lead to reduced video quality and compression efficiency.
The two-pass MPEG-4 example above also supports output to the MP4 container (replace .avi with .mp4).

####Determining bitrates with fixed output file sizes

(Desired File Size in MB - Audio File Size in MB) x 8192 kb/MB / Length of Media in Seconds (s) = Bitrate in kb/s

(3900 MB - 275 MB) = 3625 MB x 8192 kb/MB / 8830 s = 3363 kb/s required to achieve an approximate total output file size of 3900 MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment