doc:appunti:linux:video:ffmpeg
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
doc:appunti:linux:video:ffmpeg [2019/02/15 02:20] – [Manipolazione di tracce e regioni] niccolo | doc:appunti:linux:video:ffmpeg [2023/11/13 11:24] – [Doppiaggio audio con Ardour] niccolo | ||
---|---|---|---|
Line 35: | Line 35: | ||
===== Mixing videos from different sources ===== | ===== Mixing videos from different sources ===== | ||
- | 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 92: | Line 93: | ||
A 90-degree rotation involves the problem of the height / width ratio, so a more complex recipe is needed, such as the one described on the page: **[[fix_smartphone_portrait_videos]]**. | A 90-degree rotation involves the problem of the height / width ratio, so a more complex recipe is needed, such as the one described on the page: **[[fix_smartphone_portrait_videos]]**. | ||
+ | |||
+ | ==== 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 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**: | ||
+ | |||
+ | < | ||
+ | ffmpeg -f lavfi -i anullsrc=sample_rate=48000 -i timelapse_video.mp4 \ | ||
+ | -shortest -c:a libfdk_aac -b:a 128k -c:v copy timelapse_video_silence.mp4 | ||
+ | </ | ||
===== Concatenate the Clips ===== | ===== Concatenate the Clips ===== | ||
Line 252: | Line 264: | ||
</ | </ | ||
- | ====== Final rendering (re-encoding) ====== | ||
- | 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: | + | ====== Re-encoding |
- | ^ Video codec | + | We had some video clips recorded with an **SJCAM Sj8 Pro** camera with a **bad color balance and saturation** due some bad tables [[..: |
- | ^ Video filter | + | |
- | ^ Video bitrate | + | The video clips were **extracted from the original MP4 container** as **[[wp> |
- | ^ Audio codec | + | |
- | ^ Audio bitrate | + | <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} In range 0.1 to 10.0. The default value is " | ||
+ | |||
+ | 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/codec. | ||
+ | * **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. | ||
+ | | ||
+ | * **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 352: | Line 405: | ||
</ | </ | ||
- | 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 418: | Line 471: | ||
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' | ||
- | ====== Doppiaggio audio con Ardour ====== | + | ===== Aggiunta sottotitoli |
- | Per doppiare | + | Se si ha un file con i sottotitoli per un video, è possibile |
- | ===== Estrazione traccia audio originale ===== | + | < |
+ | ffmpeg -i movie.mkv -i movie.sub -c copy \ | ||
+ | -sub_charenc UTF-8 -c:s srt -metadata: | ||
+ | movie_sub.mkv | ||
+ | </ | ||
- | La traccia audio originale verrà usata come riferimento per allineare i brani musicali da abbinare al video. | + | ====== Deinterlace ====== |
- | **ATTENZIONE** alla conversione del file in formato WAV: controllare con '' | + | 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: |
< | < | ||
- | ffmpeg -i video.mp4 -vn -c:a copy audio.m4a | + | ffmpeg -i input-video.mkv -codec:a copy -vf yadif -codec:v libx264 -preset slow \ |
- | ffmpeg | + | -x264-params 'force-cfr=1: |
+ | output-video.mkv | ||
</ | </ | ||
- | ===== Conversione dei brani audio ===== | + | ====== Problem in MKV Remux ====== |
- | Per importare | + | It seems there is a bug in ffmpeg **[[https:// |
- | ===== Manipolazione di tracce e regioni ===== | + | This was the first try command line: |
- | 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 (pulsante **Start**) il sistema audio ALSA sarà impegnato in modo esclusivo, altri programmi non potranno usare l'audio. | + | < |
+ | # The resulting | ||
+ | ffmpeg -i input_file1.mkv -i input_file2.mkv \ | ||
+ | -map ' | ||
+ | -map ' | ||
+ | -codec:v copy -codec:a copy -codec:s copy \ | ||
+ | output_file.mkv | ||
+ | </ | ||
- | Con Ardour si importano le varie **tracce musicali** (menu //Session// => //Import//), se necessario il programma provvede automaticamente alla conversione del sample rate. | + | 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 | + | ====== ffmpeg: leggere la sequenza di VOB da un DVD ====== |
- | === Tagliare | + | Nella directory **VIDEO_TS** di un DVD la traccia principale è normalmente suddivisa in file numerati sequenzialmente, |
- | * Strumento forbici | + | In teoria è sufficiente concatenare i file in un solo file destinazione e quindi trattarlo come un normale file audio/ |
- | * Zoom opportuno | + | |
- | * Click sul punto: taglia immediatamente la traccia | + | |
- | * Strumento selezione, click sul pezzo da togliere, menu //Region// => //Remove// | + | |
- | === Bloccare una singola regione === | + | <code bash> |
+ | SOURCE=" | ||
+ | ffmpeg -i " | ||
+ | </ | ||
- | * Dopo averla selezionata, | + | ====== ffmpeg: impostare un ritardo sui sottotitoli durante il muxing ====== |
- | === Fade in/out === | + | Se un flusso di sottotitoli (ad esempio nel formato Picture based DVD) non indica correttamente l' |
- | * Con lo strumento "Grab mode" cliccare una regione | + | <code bash> |
- | * Trascinare il quadretto che appare all' | + | ffmpeg -i video-stream.mkv -i audio-stream.mkv -itsoffset 44.5 -i subtitles-stream.mkv ... |
+ | </ | ||
- | === | + | In generale dovrebbe essere possibile scoprire l' |
+ | |||
+ | < | ||
+ | [mpeg @ 0x55f98bb2c6c0] New subtitle stream 0:7 at pos: | ||
+ | </ | ||
+ | |||
+ | ====== Parameters for final rendering ====== | ||
+ | |||
+ | See the page **[[ffmpeg_final_rendering]]**. | ||
+ | |||
+ | ====== Doppiaggio audio con Ardour | ||
- | * //Session// => //Export// => //Export to Audio File(s)// | + | Vedere la pagina dedicata: **[[ardour_dubbing]]**. |
- | * File formats: impostare WAV 16 bit, 48 KHz | + | |
- | | + | |
- | * Channels: selezionare solo il " | + |
doc/appunti/linux/video/ffmpeg.txt · Last modified: 2024/07/25 08:12 by niccolo