Convert video files

From Ever changing code
Jump to navigation Jump to search

ffmpeg

The most basic way to use ffmpeg is to use -i yourinputfilename.ext folowed by outputfilename.ext.

That's it. This is, seriously, all you need do remember when it comes to the most basic (ab)use cases:

ffmpeg -i YourAwesomeMovie.dv YourAwesomeMovie.mp4

This basic example will make ffmpeg detect the input file type. The built-in input detection does work perfectly in 99% of all cases. This basic example will also make ffmpeg assume what audio and video formats you want for your output file based on the file extension. We asked to output the filename YourAwesomeMovie.mp4. It ends with .mp4. The .mp4 extension will make ffmpeg assume that you want a file with H.264/AVC video and AAC 2.0 audio. This happens to be the correct values for HTML5 web video.

The secret ffmpeg manual page is very long. It contains lots of detailed information about the numerous options available when using this tool. Whole books can be, and probably have been, written about the advanced options listed in the manual page.

Things you may want to tune when encoding include:

  • r to change the frame rate
  • b:v to set the video bitrate
  • b:a to set the audio bitrate

These options can have a huge impact on the quality and the file-size of the resulting file.

A general rule is: You can have low file-size OR high picture quality, but you can NOT have both. You will have to compromise.

This may give you an acceptable quality video file, depending on resolution:

ffmpeg -i YourAwesomeMovie.dv -r 25 -b:a 128k -b:v 4000k YourAwesomeMovie.mp4

..and this will NOT, because 256k is not enough bandwidth for acceptable quality even at small resolutions:

ffmpeg -i YourAwesomeMovie.dv -r 25 -b:a 64k -b:v 256k YourAwesomeMovie.mp4

Winff

Winff is GUI to ffmpeg that you can use to grab a cli command to be reused in text only environment.

Install

If you use the winff repository you need to accept the key AAFE086A

wget --quiet --output-document=- "http://winff.org/ubuntu/AAFE086A.gpg" | sudo apt-key add -

After that you need to add the repository to apt. You can do that by entering the line in /etc/apt/sources.list.d/winff.list. You will have to create that file, which can be done with the following command:

echo "deb http://winff.org/ubuntu lucid universe" | sudo tee /etc/apt/sources.list.d/winff.list

You can also just add the line to /etc/apt/sources.list .

You can install WinFF directly now by running:

sudo apt-get update && sudo apt-get install winff

If you don't run the standard ffmpeg and want to make use of the more extended original set of presets, which you can download in the download section.

In order to use the preset file supplied with the package you might need to remove the old preset file in ~/.winff/presets.xml , but by doing that you will also remove self-made presets. So if you made or edited the presets, you want to compare the files manually.

Troubleshooting
cat /etc/apt/sources.list.d/winff.list

Examples

WinFF

The below example comes from WinFF 1.5.4

  • Convert to: MPEG-4
  • Preset: MPEG-4 720p
  • File compressed: 24.1Gb 1080p to 6.41Gb 720p

C:\Program Files (x86)\WinFF>"C:\Program Files (x86)\WinFF\ffmpeg.exe" -y -i "D:\Users\green\Documents\movie.mpg" -crf 20.0 -vcodec libx264 -filter:v scale=1280:720 -preset slow -acodec libvo_aacenc -ar 48000 -b:a 128k -coder 1 -flags +loop -cmp chroma -partitions +parti4x4+partp8x8+partb8x8 -me_method hex -subq 6 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1 -threads 0 "D:\Users\green\Documents\Output\movie.mp4"

C:\Program Files (x86)\WinFF>title Converting movie.mpg (1/1)

C:\Program Files (x86)\WinFF>"C:\Program Files (x86)\WinFF\ffmpeg.exe" -y -i "D:\Users\green\Documents\movie.mpg" -crf 20.0 
-vcodec libx264 -filter:v scale=1280:720 -preset slow -acodec libvo_aacenc -ar 48000 -b:a 128k -coder 1 -flags +loop -cmp chroma 
-partitions +parti4x4+partp8x8+partb8x8 -me_method hex -subq 6 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 
-b_strategy 1 -threads 0 "D:\Users\green\Documents\Output\movie.mp4"
ffmpeg version 2.5.2 Copyright (c) 2000-2014 the FFmpeg developers
  built on Dec 30 2014 17:13:24 with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfi
g --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --
enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-lib
opencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinge
r --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --
enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-
libx265 --enable-libxavs --enable-libxvid --enable-lzma --enable-decklink --enable-zlib
  libavutil      54. 15.100 / 54. 15.100
  libavcodec     56. 13.100 / 56. 13.100
  libavformat    56. 15.102 / 56. 15.102
  libavdevice    56.  3.100 / 56.  3.100
  libavfilter     5.  2.103 /  5.  2.103
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Guessed Channel Layout for  Input Stream #0.1 : stereo
Input #0, mpeg, from 'D:\Users\green\Documents\movie.mpg':
  Duration: 03:25:36.48, start: 0.640422, bitrate: 16813 kb/s
    Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 1920x1080 [SAR 1:1 DAR 16:9], 15000 kb/s, 25 fps, 25 tbr,
 90k tbn, 50 tbc
    Stream #0:1[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels, s16, 1536 kb/s
[libx264 @ 043ba420] using SAR=1/1
[libx264 @ 043ba420] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 043ba420] profile High, level 3.1
[libx264 @ 043ba420] 264 - core 144 r2525 40bb568 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.or
g/x264.html - options: cabac=1 ref=5 deblock=1:0:0 analyse=0x3:0x111 me=hex subme=6 psy=1 psy_rd=1.00:0.00 mixed_ref=1 m
e_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_thre
ads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1
b_bias=0 direct=3 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=50 rc
=crf mbtree=1 crf=20.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.41 aq=1:1.00
Output #0, mp4, to 'D:\Users\green\Documents\Output\movie.mp4':
  Metadata:
    encoder         : Lavf56.15.102
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps,
12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc56.13.100 libx264
    Stream #0:1: Audio: aac (libvo_aacenc) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, s16, 128 kb/s
    Metadata:
      encoder         : Lavc56.13.100 libvo_aacenc
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg2video (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (pcm_dvd (native) -> aac (libvo_aacenc))
Press [q] to stop, [?] for help
frame=308412 fps=9.5 q=-1.0 Lsize= 6727234kB time=03:25:36.47 bitrate=4467.2kbits/s
video:6519117kB audio:192758kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.228836%
[libx264 @ 043ba420] frame I:1791  Avg QP:18.05  size: 84507
[libx264 @ 043ba420] frame P:157348 Avg QP:21.43  size: 31549
[libx264 @ 043ba420] frame B:149273 Avg QP:22.57  size: 10451
[libx264 @ 043ba420] consecutive B-frames: 17.3% 50.3% 12.2% 20.1%
[libx264 @ 043ba420] mb I  I16..4:  6.1% 73.6% 20.3%
[libx264 @ 043ba420] mb P  I16..4:  3.6%  0.0%  6.5%  P16..4: 49.3% 22.7% 11.8%  0.0%  0.0%    skip: 6.2%
[libx264 @ 043ba420] mb B  I16..4:  7.2%  0.0%  1.5%  B16..8: 23.2%  8.6%  0.7%  direct:16.1%  skip:42.7%  L0:37.5% L1:4
6.9% BI:15.6%
[libx264 @ 043ba420] 8x8 transform intra:4.3% inter:62.0%
[libx264 @ 043ba420] direct mvs  spatial:99.5% temporal:0.5%
[libx264 @ 043ba420] coded y,uvDC,uvAC intra: 51.8% 86.9% 56.9% inter: 30.1% 48.8% 8.9%
[libx264 @ 043ba420] i16 v,h,dc,p: 31% 17% 29% 23%
[libx264 @ 043ba420] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 17% 18%  6%  6%  7%  7%  8%  8%
[libx264 @ 043ba420] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25% 16% 18%  6%  8%  8%  7%  7%  4%
[libx264 @ 043ba420] i8c dc,h,v,p: 53% 16% 23%  8%
[libx264 @ 043ba420] Weighted P-Frames: Y:3.1% UV:1.3%
[libx264 @ 043ba420] ref P L0: 59.9% 15.7% 14.0%  4.9%  4.2%  1.2%  0.0%
[libx264 @ 043ba420] ref B L0: 79.8% 13.6%  5.5%  1.1%
[libx264 @ 043ba420] ref B L1: 97.0%  3.0%
[libx264 @ 043ba420] kb/s:4329.00
[libvo_aacenc @ 04e00060] 1 frames left in the queue on closing

C:\Program Files (x86)\WinFF>pause
Press any key to continue . . .

Verify video integrity

This snippet below does check video integrity and writes errors into a log.

#!/bin/bash -exv

TIMESTAMP=$(date +"%Y%m%dT-%H%M.%S")

find . -type f -iname "*.mp4" | sort -V | while read -r LINE        ; do
  echo "$LINE" | tee -a  error-${TIMESTAMP}.log                     ;
  ffmpeg -i "$LINE" -f null - | tee -a error-${TIMESTAMP}.log 2>&1  ;
done                                                                ;
# LINE="$(echo "$LINE" | sed -r 's/^\W+//g')"

References