Help:Converting video: Difference between revisions

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Tags: Reverted Mobile edit Mobile web edit
Blanked the page
Tags: Blanking Reverted Mobile edit Mobile web edit
Line 1: Line 1:
== Multi-platform command-line conversion tools ==
=== ffmpeg ===
[[w:FFmpeg|FFmpeg]] is the Swiss army knife of video conversion. It's a highly flexible software suite that brings together a large amount of tools for handling video, audio, and other multimedia. It provides a command-line program "ffmpeg", with which you can convert also to [[w:WebM|WebM]]. See FFmpeg wiki's [https://trac.ffmpeg.org/wiki/Encode/VP9 VP9 Encoding Guide] for more information on its usage with VP9 specifically. For even more information on available command-line arguments specific to VP9 encoding, see FFmpeg's built-in help with the command <code>ffmpeg -h encoder=libvpx-vp9</code>

==== .webm ====
The recommended way to use VP9 is to utilise two-pass constant quality encoding. This is a combination of two modes (2-pass and constant quality) that have traditionally been separate approaches, and is recommended due to some quality-enhancing features in the libvpx encoder only being available in two-pass mode. Two-pass mode goes through the video twice, first to analyze it and second to use the collected data to encode the video efficiently at a certain set average bitrate. Constant quality mode aims to achieve a certain perceptual quality level for all parts of the video. Two-pass constant quality mode does an analysis pass and then encodes the video with a quality level set by the user. The quality level is set with the <code>-crf</code> switch and also requires that <code>-b:v</code> is set to 0 (zero).

There are other useful options available (not all of them specific to VP9), some of which are used in the example commands below and explained further in the bullet points below.

Two-pass encoding is done by running two commands, one for each pass, and specifying which pass you're running with the <code>-pass</code> switch. The first pass creates an analysis log file named <code>ffmpeg2pass-0.log</code>, which can be deleted after the second pass is done.

Since the command lines of different platforms use slightly different syntax (Windows uses NUL instead of /dev/null), the commands for each platform are listed separately.

===== Linux and OS X command line =====

:First pass:
:: <code>ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 1 -row-mt 1 -an -f webm -y /dev/null</code>

:Second pass:
:: <code>ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 2 -row-mt 1 -c:a libopus output.webm</code>

The two commands can be run back-to-back by combining them with two <code>&</code> signs, like so:

:: <code>ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 1 -row-mt 1 -an -f webm -y /dev/null && ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 2 -row-mt 1 -c:a libopus output.webm</code>

'''Create a script''':
When you frequently convert videos it can be handy to create a script video2webm:
<syntaxhighlight lang="bash">
#!/bin/bash
# Convert video file to webm
# Author: Geert Van Pamel
# Parameters:
# P1: input file name

if [[ -z "$1" ]] ;then
echo "Input file missing"
exit 1
fi

input="$1"
output=$(basename "$1").webm

ffmpeg -i "$input" -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 1 -row-mt 1 -an -f webm -y /dev/null &&
ffmpeg -i "$input" -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 2 -row-mt 1 -c:a libopus "$output"

</syntaxhighlight>
Then simply execute:

:: <code>video2webm file.mp4</code>
A file file.mp4.webm is created in the current directory.

===== Linux using VAAPI on Intel devices =====
Those running Linux on a modern Intel-based machine (Kaby/Coffee Lake, 9th generation or later) may see significant speedup by using the Video Acceration API (VAAPI) with ffmpeg. This takes advantage of the Intel QuickSync hardware in the integrated GPU (iGPU) of the Intel UHD Graphics 630 or the Iris Plus. Speeds of up to 8x can be seen using hardware VP9 encoding, versus pure CPU-only encoding speeds of 1-2x.

* [https://github.com/intel/intel-vaapi-driver Installation of the VAAPI driver from Github]

The following command can be invoked to tap into the VP9 VAAPI driver:

: <code>ffmpeg -vaapi_device /dev/dri/renderD128 -i input.mp4 -vf 'format=nv12,hwupload' -c:v vp9_vaapi -c:a libopus -b:a 96K -bsf:v vp9_raw_reorder,vp9_superframe output.webm</code>

* [https://trac.ffmpeg.org/wiki/Hardware/VAAPI More details on ffmpeg VP9 acceleration]

===== Windows command line =====

:First pass:
:: <code>ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 1 -row-mt 1 -an -f webm -y NUL</code>

:Second pass:
:: <code>ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 2 -row-mt 1 -c:a libopus output.webm</code>

:Combined:
::<code>ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 1 -row-mt 1 -an -f webm -y NUL && ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -pass 2 -row-mt 1 -c:a libopus output.webm</code>

* The target quality of <code>-crf</code> is specified on a scale from 0 to 63, with smaller values indicating higher quality. The default value is 30, which is sufficient for HD video. In general, the higher the video resolution the higher the value can be set since VP9 is more efficient at higher resolutions. This is useful to keep in mind when encoding resolutions higher than 1080p.
* The audio quality, when not manually determined by the <code>-b:a</code> switch, is set to 96k by default. This is more than sufficient for most use cases as Opus is a very efficient audio codec. However, if your use case requires it you may wish to set it to a different value.
* If you wish your video to not have audio, you can remove it by using the <code>-an</code> flag during the second pass. This is always done with the first pass since re-encoding audio would slow it down for no benefit. Furthermore, if you wish to specify the number audio channels yourself (for example, for converting 5.1 surround audio to stereo, or even mono), you can use the <code>-ac</code> flag followed by the number of audio channels desired (ie. <code>-ac 2</code> for stereo).
* The log file created during the first pass can be re-used and isn't actually specific to the quality level specified during the first pass. So if you encoded a video with a certain quality level but aren't happy with the result, as long as you didn't delete the log file, you can just re-run the second pass with a different quality setting and save time by not re-running the first pass.
* The <code>-row-mt 1</code> switch tells the encoder to use row-based multithreading. This can speed up encoding significantly on computers with more than 4 CPU cores/threads, resulting in a speed-up of 2x or more depending on the video resolution and the amount of CPU cores available. This option is set to 0 by default (as of January 2019) and thus needs to be set manually.
* If you're using a pre-2018 version of FFmpeg, you may find VP9 encoding performance lacking. This is due to the aforementioned <code>-row-mt</code> multithreading feature being introduced in version 1.7.0 of libvpx (the VP8/VP9 encoding library used by FFmpeg), released on January 25th, 2018. FFmpeg added support for <code>-row-mt</code> in FFmpeg 3.4.
:Furthermore, libvpx versions prior to 1.7.0 also require setting other multithreading features manually. These are the amount of threads, set with the <code>-threads</code> switch, and the amount of tile columns to use, set with the <code>-tile-columns</code> switch. The amount of threads should be set to the amount of processor threads made available by your CPU. The number of tile columns possible is determined by the resolution of the video and also affects the maximum amount of encoding threads the encoder can use. Below is a table of the recommended tile-columns values for various resolutions and the maximum amount of threads made possible by them, taken from [https://developers.google.com/media/vp9/settings/vod/ Google's VP9 encoding guide.]

:{| class="wikitable"
|-
! Video resolution
! <code>-tile-columns</code> value
! Number of threads
|-
| 320x240
| 1
| 2
|-
| 640x360
| 2
| 4
|-
| 640x480
| 2
| 4
|-
| 1280x720
| 4
| 8
|-
| 1920x1080
| 4
| 8
|-
| 2560x1440
| 8
| 16
|-
| 3840x2160
| 16
| 24
|}

==== .ogv ====
'''NOTE! Theora is older and significantly less efficient than VP9, leading to higher file sizes (and thus longer upload times and higher storage space use) at the same quality compared to newer formats. In general the use of Theora for new uploads should be avoided and VP9 used instead.''' Nevertheless, examples for converting to Theora are provided below.

Theora video with Vorbis audio:
: <code>ffmpeg -i demo.mp4 -c:v libtheora -q:v 7 -c:a libvorbis -q:a 6 demo.ogv</code>

Theora video with [[w:FLAC|FLAC]] audio:
: <code>ffmpeg -i demo.mp4 -c:v libtheora -q:v 7 -c:a flac -sample_fmt s16 demo.ogv</code>

Theora video with audio removed:
: <code>ffmpeg -i demo.mp4 -c:v libtheora -q:v 7 -an demo.ogv</code>

The quality of the video and audio is specified with <code>-q:v</code> and <code>-q:a</code> respectively (0–10, higher is better). See [https://trac.ffmpeg.org/wiki/TheoraVorbisEncodingGuide TheoraVorbisEncodingGuide] for more information.

==== Note on pixel formats ====
Note that by default, FFmpeg will attempt to convert video losslessly from one pixel format to another. Since pixel formats other than <code>yuv420p</code> aren't supported by all browsers, this can become problematic when encoding HDR content or when making a video from a series of images. [[w:Motion JPEG|Motion JPEG]] files, for example, will be converted by ffmpeg to <code>yuv422p</code>, and PNG files can end up converted to RGB. Not only can these videos cause problems with browser playback, but usually they also end up much larger when encoding them using the regular quality values.

In cases like this, adding <code>-pix_fmt yuv420p</code> to the FFmpeg command will ensure wide playback compatibility by converting the video to the <code>yuv420p</code> format.

==== Rotating videos ====
Videos can be rotated by using the parameter <code>-vf "transpose=<direction>"</code> where "direction" is one of <code>cclock_flip</code>, <code>clock</code>, <code>cclock</code> or <code>clock_flip</code> (respectively, 90 degrees counter-clockwise and vertical flip, 90 degrees clockwise, 90 degrees counter-clockwise, and 90 degrees clockwise and vertical flip). To rotate 180 degrees, try <code>-vf "vflip,hflip"</code>. The video stream must be transcoded during this process, but the audio stream can be copied with <code>-c:a copy</code> if it is already in a suitable codec. It's recommended to specify video transcoding options as in the sections above since the FFmpeg defaults will likely result in a severe drop in quality.

For example, to rotate an existing WebM video 90 degrees clockwise while keeping a decent quality, run:
:<code>ffmpeg -i input.webm -c:a copy -c:v libvpx-vp9 -b:v 0 -crf 30 -vf "transpose=clock" output.webm</code>

=== avconv ===
[[w:Libav|Libav]] is a fork of FFmpeg providing the similar command-line program "avconv".
It appears that the quality is not as good as oggconvert.
Also the first and last seconds of an mp3 file can be lost (which is certainly not OK).
: <code>apt-get install libav-tools</code>

* To extract 325 seconds of audio starting at second 475 with a bitrate of 64 Mbit/s:
: <code>avconv -i input-file.mp3 -b 64k -ss 475 -t 325 output-file.mp3</code>
The input file bit rate should be higher or equal to the output file bit rate... otherwise bad audio quality will be the result.

* To convert multiple mp3 files at once to ogg format:
: <code>for i in *.mp3 ;do avconv -i ${i} ${i:%mp3}ogg ;done</code>

* To remove the (bad) sound from a video:
: <code>avconv -i infile -c copy -an outfile</code>

=== ffmpeg2theora ===
[[File:Ffmpeg2theora example conversion.png|thumb|200px|ffmpeg2theora command-line tool converting a video file]]
[http://www.v2v.cc/~j/ffmpeg2theora/index.html ffmpeg2theora] was a popular converter recommended at the Theora project page. It is a cross-platform command-line tool. The latest version is 0.30, released in January 2016.

In most cases you will only need a simple command indicating the name of the file to convert. (Video and audio quality is set to max.)

; Command line examples
: <code>ffmpeg2theora file.avi -v 10 -a 10</code>
: <code>ffmpeg2theora file.avi --noaudio -v 10</code>


=== MPlayer ===
[http://www.mplayerhq.hu MPlayer] is an open source multimedia player also being capable to transcode files but having the advantage of supporting both a GUI as well as the command line; available for most operating systems including Windows, Linux, and macOS. The MPlayer included by some [[w:en:SMPlayer|SMPlayer]] distributions for Windows does not contain <tt>mencoder.exe</tt>.

=== OggConvert ===
OggConvert allows you to produce ogg/ogv output files. To install:
: <code>apt-get install oggconvert</code>

=== VLC media player ===
[//www.videolan.org/vlc/ VLC] is a GUI version media player with conversion capabilities. A helpful tutorial can be found at [https://archive.flossmanuals.net/ogg-theora/encoding/vlc.html TheoraCookbook]. Encoding using the GUI requires a targeted bitrate which yields inferior quality than encoding for a specified video quality.

=== XiphQT ===
Xiph's [//xiph.org/quicktime/ QuickTime Components] can be used to add OGG support to QuickTime Pro, under both macOS (versions 10.3.9 to 10.11) and Windows.

Download and install the component, and it will add a "Movie to OGG" option to the File->Export dialog. On macOS, you can only do this using QuickTime 7, and you can download QuickTime 7 by visiting [//www.apple.com/quicktime/ Apple].

=== youtube-dl ===
[https://ytdl-org.github.io/youtube-dl/index.html youtube-dl] is an open-source command line program that can download videos from YouTube and a selection of other video sites, such as archive.org or Vimeo (full list [https://ytdl-org.github.io/youtube-dl/supportedsites.html here]). If installed alongside [[:en:FFmpeg|FFmpeg]] or [[:en:Libav|Libav]], it can transcode downloaded videos to a free format. The following examples will show how this can be done, using the YouTube video [https://www.youtube.com/watch?v=dPZTh2NKTm4 What is Creative Commons?] by the Wikimedia Foundation as an example.

Many videos on YouTube already have a WebM version available, using the VP9 and Opus codecs. To check:
:<code>youtube-dl -F dPZTh2NKTm4</code> (note: you can specify the full URL or just the video ID)

The output of the above command shows the available formats, arranged by file extension and resolution:

{{collapse top|title=Output of <tt>youtube-dl -F</tt>}}
<pre>
$ youtube-dl -F dPZTh2NKTm4

[youtube] dPZTh2NKTm4: Downloading webpage
[youtube] dPZTh2NKTm4: Downloading video info webpage
[info] Available formats for dPZTh2NKTm4:
format code extension resolution note
249 webm audio only tiny 56k , opus @ 50k (48000Hz), 530.03KiB
250 webm audio only tiny 76k , opus @ 70k (48000Hz), 722.58KiB
140 m4a audio only tiny 127k , m4a_dash container, mp4a.40.2@128k (44100Hz), 1.27MiB
251 webm audio only tiny 144k , opus @160k (48000Hz), 1.35MiB
160 mp4 256x144 144p 112k , avc1.4d400c, 30fps, video only, 646.32KiB
278 webm 256x144 144p 119k , webm container, vp9, 30fps, video only, 746.38KiB
133 mp4 426x240 240p 246k , avc1.4d4015, 30fps, video only, 1.31MiB
242 webm 426x240 240p 291k , vp9, 30fps, video only, 1.50MiB
243 webm 640x360 360p 562k , vp9, 30fps, video only, 2.85MiB
134 mp4 640x360 360p 635k , avc1.4d401e, 30fps, video only, 3.27MiB
244 webm 854x480 480p 1048k , vp9, 30fps, video only, 5.05MiB
135 mp4 854x480 480p 1181k , avc1.4d401f, 30fps, video only, 5.99MiB
247 webm 1280x720 720p 2066k , vp9, 30fps, video only, 10.12MiB
136 mp4 1280x720 720p 2387k , avc1.4d401f, 30fps, video only, 11.70MiB
302 webm 1280x720 720p60 3243k , vp9, 60fps, video only, 16.05MiB
298 mp4 1280x720 720p60 3483k , avc1.4d4020, 60fps, video only, 17.49MiB
248 webm 1920x1080 1080p 3604k , vp9, 30fps, video only, 17.90MiB
137 mp4 1920x1080 1080p 4368k , avc1.640028, 30fps, video only, 20.92MiB
303 webm 1920x1080 1080p60 5429k , vp9, 60fps, video only, 27.01MiB
299 mp4 1920x1080 1080p60 5800k , avc1.64002a, 60fps, video only, 29.41MiB
271 webm 2560x1440 1440p 11997k , vp9, 30fps, video only, 52.51MiB
308 webm 2560x1440 1440p60 15209k , vp9, 60fps, video only, 74.46MiB
313 webm 3840x2160 2160p 24605k , vp9, 30fps, video only, 112.91MiB
315 webm 3840x2160 2160p60 32659k , vp9, 60fps, video only, 155.65MiB
18 mp4 640x360 360p 536k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 5.34MiB
22 mp4 1280x720 720p 1301k , avc1.64001F, mp4a.40.2@192k (44100Hz) (best)
</pre>
{{collapse bottom}}

Individual formats can be downloaded by specifying the format code in the first column:
:<code>youtube-dl -f 22 dPZTh2NKTm4</code>

Video and audio can be downloaded together and combined into a single file with FFmpeg or Libav, if installed:
:<code>youtube-dl -f 315+251 dPZTh2NKTm4</code>

To download the best available free format for both audio and video without needing to know the format codes:
:<code>youtube-dl --prefer-free-formats dPZTh2NKTm4</code>

Note that youtube-dl considers MP4 a free format, but [[Commons:Requests for comment/MP4 Video|MP4 video is not supported on Commons]]; to download WebM instead:
:<code>youtube-dl -f 'bestvideo[ext=webm]+bestaudio[ext=webm]' dPZTh2NKTm4</code>

WebM may not always be available, but FFmpeg or Libav can be used to transcode the video once downloaded. For example:
:<code>youtube-dl -f best --recode-video webm dPZTh2NKTm4</code> (<code>-f best</code> is used here to force a single file MP4 download; it won't always be necessary)

If either FFmpeg or Libav is on your PATH, youtube-dl will automatically find and use it. Otherwise, you can tell it where to find the conversion program with the <code>--ffmpeg-location</code> option.

See the [https://github.com/ytdl-org/youtube-dl/blob/master/README.md README] for more on what youtube-dl can do (like batch downloading or audio extraction), as well as example commands and frequently asked questions.

Revision as of 04:56, 30 June 2023