doc:appunti:linux:video:ffmpeg
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
doc:appunti:linux:video:ffmpeg [2019/03/16 07:47] – [Rotate] niccolo | doc:appunti:linux:video:ffmpeg [2023/01/07 15:21] – [AVC (x264) is better than ASP (xvid4)] niccolo | ||
---|---|---|---|
Line 37: | Line 37: | ||
Sometimes we needed to mix video clips originating from different sources. To apply our simple cut-and-paste recipe (wich preserve the original video quality, without re-encoding), | Sometimes we needed to mix video clips originating from different sources. To apply our simple cut-and-paste recipe (wich preserve the original video quality, without re-encoding), | ||
+ | One annoying effect of videos created by joining clips with different encoding parameters, is seen in **mplayer**: | ||
==== Normalize (Resize and Re-encode) ==== | ==== Normalize (Resize and Re-encode) ==== | ||
Line 95: | Line 96: | ||
==== Add an Audio Track ==== | ==== Add an Audio Track ==== | ||
- | The **time-lapse videos** taken by the Xiaomi Yi Action camera **do not have an audio track**. This causes problems when cutting and concatenating clips with the the recipes presented below: you can see the play (with **mplayer**) which **freezes for several seconds** at the joining point of a clip with no audio and a clip with audio. | + | The **time-lapse videos** taken by the Xiaomi Yi Action camera **do not have an audio track**. This causes problems when cutting and concatenating clips with the recipes presented below: you can see the play (with **mplayer**) which **freezes for several seconds** at the joining point of a clip with no audio and a clip with audio. |
So I add a silence audio track to theese clips with **ffmpeg**: | So I add a silence audio track to theese clips with **ffmpeg**: | ||
Line 267: | Line 268: | ||
The video stream recorded by the Xiaomi Yi camera is **1920x1080 pixels** at a variable bitrate of **12.0 Mb/s**. Because we watch it on a simple TV set capable only of 1366x768 pixels, we we re-encode it with the following settings: | The video stream recorded by the Xiaomi Yi camera is **1920x1080 pixels** at a variable bitrate of **12.0 Mb/s**. Because we watch it on a simple TV set capable only of 1366x768 pixels, we we re-encode it with the following settings: | ||
- | ^ Video codec | XViD4 | | + | ^ Video codec |
- | ^ Video filter | + | ^ Video filter |
- | ^ Video bitrate | + | ^ Basic x264 | Preset: **slow** (or less), Tuning: **film**, Profile: **High**, IDC Level: **Auto** |
- | ^ Audio codec | Lame MP3 | | + | ^ Video encoding |
- | ^ Audio bitrate | + | ^ Audio codec |
+ | ^ Audio bitrate | ||
+ | |||
+ | We can use **Avidemux** to make the final rendering (re-encoding). For a **command line only** solution you can consider **ffmpeg** to perfomr the re-encoding and to make the merge (mux) all into a Matroska container. | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/sh | ||
+ | TITLE=" | ||
+ | ffmpeg \ | ||
+ | -i " | ||
+ | -i ' | ||
+ | -map ' | ||
+ | -metadata title=" | ||
+ | -metadata: | ||
+ | -metadata: | ||
+ | -filter:v " | ||
+ | -vcodec ' | ||
+ | -acodec copy \ | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Re-encoding with tonal correction ====== | ||
+ | |||
+ | We had some video clips recorded with an **SJCAM Sj8 Pro** camera with a **bad color balance and saturation** due some bad tables [[..: | ||
+ | |||
+ | The video clips were **extracted from the original MP4 container** as **[[wp> | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/sh | ||
+ | # | ||
+ | # Re-encode video clips in MPEG transport stream (MPEG-TS) format applying | ||
+ | # some saturation and gamma correction. | ||
+ | # | ||
+ | # saturation: | ||
+ | # gamma_{r|g|b} | ||
+ | |||
+ | INPUT=" | ||
+ | OUTPUT=" | ||
+ | EQ_FILTER=" | ||
+ | |||
+ | # Produces MPEG segments like the ones produced by the SJCAM SJ8Pro: | ||
+ | ffmpeg -i " | ||
+ | -vf " | ||
+ | -codec:v libx264 \ | ||
+ | -preset veryslow -profile:v main -level:v 4.2 -pix_fmt yuvj420p \ | ||
+ | -x264-params ' | ||
+ | -keyint_min 8 -brand avc1 -f 3gp \ | ||
+ | -bsf:v h264_mp4toannexb -f mpegts \ | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | The gamma correction for the three RGB channels was determined with the GIMP, using the //Colors// => //Levels// => //Pick the gray point for all channels// tool. The use of MPEG-TS clips allowed the montage of the final video by just concatenating them. | ||
+ | |||
+ | ===== AVC (x264) is better than ASP (xvid4) ===== | ||
+ | |||
+ | See this page: **[[https:// | ||
+ | |||
+ | * **MPEG-4 Part 2 ASP** (Advanced Simple Profile) | ||
+ | * Support only 16x16 block size. | ||
+ | * Implemented by the Xvid library/ | ||
+ | * **MPEG-4 Part 10 AVC** (Advanced Video Coding) | ||
+ | * Support variable motion compensation block sizes. | ||
+ | * Implemented by the x264 library. | ||
+ | |||
+ | If you want to tweak with x264 codec options, here are some hints on the parameters meaning: | ||
+ | |||
+ | * **Preset**: Use the **slow** preset (or less) to achieve best compression ratio, at the expense of more time to encode. | ||
+ | * **Tuning**: Use the **film** option for real life scenes. | ||
+ | * **Profile**: | ||
+ | * **IDC Level**: Leave this parameter to **Auto**, setting it to higer values does not increase the video quality, but imposes higher constraints to the decoder. I.e. to decode 1280×1024@42 it is sufficient the 3.1 IDC level (see [[wp> | ||
====== More on ffmpeg Command Line ====== | ====== More on ffmpeg Command Line ====== | ||
Line 363: | Line 434: | ||
</ | </ | ||
- | the CSV format can be controlled by several options, e.g. if you want to know the key for each filed use: | + | the CSV format can be controlled by several options, e.g. if you want to print each field as a **key=val** pair, use: |
< | < | ||
Line 429: | Line 500: | ||
Un test con un video molto mosso (telecamera impugnata a mano su motocicletta in movimento) con **zoom=1** e **smoothing=30** ha ridotto il film a circa il 77% dell' | Un test con un video molto mosso (telecamera impugnata a mano su motocicletta in movimento) con **zoom=1** e **smoothing=30** ha ridotto il film a circa il 77% dell' | ||
- | ====== | + | ===== Aggiunta sottotitoli |
- | + | ||
- | Per doppiare un video sostitutendo o mixando l' | + | |
- | + | ||
- | ===== Estrazione traccia audio originale ===== | + | |
- | + | ||
- | La traccia audio originale verrà usata come riferimento per allineare i brani musicali da abbinare al video. | + | |
- | **ATTENZIONE** alla conversione del file in formato WAV: controllare | + | Se si ha un file con i sottotitoli per un video, |
< | < | ||
- | ffmpeg -i video.mp4 -vn -c:a copy audio.m4a | + | ffmpeg -i movie.mkv -i movie.sub |
- | ffmpeg | + | -sub_charenc UTF-8 -c:s srt -metadata: |
+ | movie_sub.mkv | ||
</ | </ | ||
- | ===== Conversione dei brani audio ===== | + | ====== Deinterlace ====== |
- | Per importare in Ardour i brani musicali vanno convertiti in WAV quelli non direttamente supportati (es. i file MP3 e le tracce M4A solitamente estratte dai video MP4). Per questo si può utilizzare | + | We can use the video filter named **yadif** (//yet another deinterlacing filter//). In this example the result was encoded in MPEG-4 AVC using the libx264 library, forcing one keyframe every 8 frames: |
< | < | ||
- | sox file.mp3 file.wav | + | ffmpeg -i input-video.mkv -codec:a copy -vf yadif -codec:v libx264 -preset slow \ |
+ | -x264-params ' | ||
+ | output-video.mkv | ||
</ | </ | ||
- | ===== Manipolazione di tracce e regioni | + | ====== Problem in MKV Remux ====== |
- | Con Ardour si inizia un **nuovo progetto**, impostare **48 kHz, 32 bit float, stereo**, che sono i parametri che vanno per la maggiore nei video MP4 in alta risoluzione. Quando il progetto è avviato | + | It seems there is a bug in ffmpeg |
- | Con Ardour si **importano** le varie **tracce musicali** (menu //Session// => // | + | This was the first try command line: |
- | Attenzione al **fader** di ogni traccia: se per caso viene spostato con il mouse, fare **Ctrl-click** per resettarlo al **valore predefinito di 0 dB**. | + | < |
+ | # The resulting video is broken. | ||
+ | ffmpeg -i input_file1.mkv -i input_file2.mkv \ | ||
+ | -map '0:v: | ||
+ | -map ' | ||
+ | -codec:v copy -codec:a copy -codec:s copy \ | ||
+ | output_file.mkv | ||
+ | </ | ||
- | {{..:ardour: | + | The workaround was to extract each individual stream, and mux then together: |
- | Ogni **traccia** può essere manipolata separatamente (es. spostata nella timeline, ecc.), inizialmente la traccia è costituita da un' | + | < |
+ | ffmpeg -i input_file1.mkv -map 0:v:0 -codec:v copy input-v_env.mkv | ||
+ | ffmpeg -i input_file1.mkv -map 0:a:0 -codec:a copy input-a_ita.mkv | ||
+ | ffmpeg -i input_file2.mkv -map 0:a:0 -codec:a copy input-a_eng.mkv | ||
+ | ffmpeg -i input_file2.mkv -map 0:s:0 -codec:s copy input-s_eng.mkv | ||
+ | ffmpeg \ | ||
+ | -i input-v_env.mkv \ | ||
+ | -i input-a_ita.mkv \ | ||
+ | -i input-a_eng.mkv \ | ||
+ | -i input-s_eng.mkv \ | ||
+ | -codec:v copy -codec:a copy -codec:s copy \ | ||
+ | -map ' | ||
+ | output_file.mkv | ||
+ | </ | ||
- | === Spostare === | ||
- | === Tagliare un pezzo === | + | ====== |
- | + | ||
- | * Strumento forbici | + | |
- | * Zoom opportuno | + | |
- | * Click sul punto: taglia immediatamente la traccia | + | |
- | * Strumento selezione, click sul pezzo da togliere, menu // | + | |
- | + | ||
- | === Bloccare una singola regione === | + | |
- | + | ||
- | * Dopo averla selezionata, | + | |
- | + | ||
- | === Fade in/out === | + | |
- | + | ||
- | * Con lo strumento "Grab mode" cliccare una regione | + | |
- | * Trascinare il quadretto che appare all' | + | |
- | + | ||
- | === Esportare tutto il lavoro in WAV === | + | |
- | * //Session// => //Export// => //Export to Audio File(s)// | + | Vedere la pagina dedicata: **[[ardour_dubbing]]**. |
- | * **File formats**: impostare **WAV 16 bit, 48 KHz**. Eventualmente si può abilitare il **Normalize**, | + | |
- | * **Time Span**: Controllare che sia pari alla lunghezza originale. Se c'è qualcosa di troppo aggiustare il tag **end** nella barra **Location markers**. | + | |
- | * **Channels**: | + |
doc/appunti/linux/video/ffmpeg.txt · Last modified: 2024/07/25 08:12 by niccolo