[Info] ffmpeg encoding

Post tutorials, HOWTO's and other useful resources here.
User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Howto: Popular AppImages one click away

Post#1 by Rava » 14 Jan 2021, 20:54

Some videos are cut good with LosslessCut version 3.30.0 - and some are not.

Like in these examples:
14-00.00.11.052-00.22.52.202-seg1.mp4
14-00.00.13.471-00.22.52.202-seg1.mp4

The first has still the annoying part at the beginning in it, and the 2nd one cuts off part of the main video. Both are the only positions LosslessCut v3.30.0 lets me choose closest to the target area (see below for the best cut position) - I used the "key frame" for the first, and the "cursor right/left" keys for the 2nd.

Since I see no way to increase or decrease the video by single frames in LosslessCut (you use the cursor keys in avidemux to go frame by single frame - maybe you just use other keys I am unaware of in LossLessCut), I switched to avidemux_2.6.6.AppImage - and use it only to determine where to cut - I copy the position code into the console and used this code

Code: Select all

time ffmpeg -v error -ss 0:10.135 -i 14.mp4 -codec copy 14_00.00.10.135.mp4
and the result is perfect: all cut out what I do not want while keeping everything of the video in it, a thing LossLessCut is unable doing with some videos. (Unless silly me just not knows how to navigate frame by frame)

Please be aware that using "-v error" should only be done when you know there is no important info omitted, when unsure leave out the "-v error" part.
Cheers!
Yours Rava

User avatar
M. Eerie
Moderator
Moderator
Posts: 620
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

Howto: Popular AppImages one click away

Post#2 by M. Eerie » 15 Jan 2021, 13:36

Rava wrote:
14 Jan 2021, 20:54
Both are the only positions LosslessCut v3.30.0 lets me choose closest to the target area
Take in mind that LosslessCut uses ffmpeg to do its operations. In other words, is just another ffmpeg GUI, maybe with some added tricks.

From my own experiences, ffmpeg command-line produced clips are also based on keyframes and stick to the nearest keyframe rather than exact milliseconds timings. If you want an exact cut I presume you'll need to re-encode (i.e. not -c copy). This is due to the way most videos are encoded.

Some recommendations here and here.

You'll see "-ss" it is used twice in the command (fast seek and then accurate seek).
Rava wrote:
14 Jan 2021, 20:54
time ffmpeg -v error -ss 0:10.135 -i 14.mp4 -codec copy 14_00.00.10.135.mp4
Also, I'd recommend to add "-map 0" to ensure all tracks are kept in output file. And since you are using "-c copy", "-avoid_negative_ts 1" is advised.

Code: Select all

time ffmpeg -v error -ss 0:10.135 -i 14.mp4 -map 0 -codec copy -avoid_negative_ts 1 4_00.00.10.135.mp4
(Once more, I think this is out of topic and should be moved to a video/commands relating thread)
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[Info] ffmpeg encoding

Post#3 by Rava » 15 Jan 2021, 18:53

M. Eerie wrote:
15 Jan 2021, 13:36
Some recommendations here and here.

You'll see "-ss" it is used twice in the command (fast seek and then accurate seek).
Rava wrote:
14 Jan 2021, 20:54
time ffmpeg -v error -ss 0:10.135 -i 14.mp4 -codec copy 14_00.00.10.135.mp4
Also, I'd recommend to add "-map 0" to ensure all tracks are kept in output file. And since you are using "-c copy", "-avoid_negative_ts 1" is advised.
Thanks for the heads up on
using -ss twice for accurate search
add "-map 0"
and
"-avoid_negative_ts 1" together with "-c copy"

:good:
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[Info] ffmpeg encoding

Post#4 by Rava » 23 Jan 2021, 15:50

Thanks to M. Eerie I could enhance my help.ffmpeg script.
The original contains escape sequences for colour highlighting which cannot be shared here
via [ code ]

Code: Select all

root@porteus:~# file /usr/local/bin/help.ffmpeg 
/usr/local/bin/help.ffmpeg: POSIX shell script, UTF-8 Unicode text executable, with escape sequences
I will share help.ffmpeg only as its text output
help.ffmepeg V0.8
extract audio (compressions artefacts free, sans decoding)
ffmpeg -i name.mp4 -codec copy name.aac or n.mp3 or n.mp2 or n.ac3
avconv -i input.mp4 -acodec copy output.aac # <- should be quicker, when part of ffmpeg txz, that is!

Extracting an ffmetadata file
ffmpeg -i INPUT -f ffmetadata FFMETADATAFILE

Reinserting edited metadata information from the FFMETADATAFILE file
ffmpeg -i INPUT -i FFMETADATAFILE -map_metadata 1 -codec copy OUTPUT

Crop
ffmpeg -i IF -vf crop=1013:1092:533:54 IF_crop.mp4
width:height:X-Offset:Y-Offset

Images:
time ffmpeg -v error -ss START -i IF -vf yadif -t DURATION -f image2 /mnt/outpath/-%03d.png
time ffmpeg -v error -ss -i -vf yadif -t -f image2 /mnt/outpath/-%03d.png

Video out of images:
ffmpeg -start_number 11 -f image2 -i name-%03d.png name.mp4
-start_number only when sequence not starts with 001.

Cut:
time ffmpeg -v error -ss START -i IF -map 0 -codec copy -avoid_negative_ts 1 -t DURATION OF
time ffmpeg -v error -ss -i -map 0 -codec copy -avoid_negative_ts 1 -t

Cut with fast search & accurate search:
time ffmpeg -v error -ss START-minus-10seconds -i IF -ss 0:00:10 -map 0 -codec copy -avoid_negative_ts 1 -t DURATION OF
time ffmpeg -v error -ss -i -ss 0:00:10 -map 0 -codec copy -avoid_negative_ts 1 -t

Change aspect-ratio in container-Level (not frame-level; sans encoding)
time ffmpeg -v error -i IF -map 0 -codec copy -avoid_negative_ts 1 -aspect RATIO OF
e.g.
time ffmpeg -v error -i IF.avi -map 0 -codec copy -avoid_negative_ts 1 -aspect 16:9 OF.avi
time ffmpeg -v error -i -map 0 -codec copy -avoid_negative_ts 1 -aspect
The first line is with explanation,
the 2nd just with spaces for copying the code and inserting at the appropriate spot what is needed.

IF =InFile , OF = OutFile

%03d is a sequence starting with 001 and could go up to 999. If you need even larger numbers (e.g. video out of images or images out of video) use %04d or %05d. :)
Cheers!
Yours Rava

donald
Full of knowledge
Full of knowledge
Posts: 2064
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

[Info] ffmpeg encoding

Post#5 by donald » 23 Jan 2021, 19:46

Hi Rava....may I?

ffmpeg
If you just want to copy the audio stream without re-encoding just do:
[Example]
ffmpeg -i video.mp4 -vn -acodec copy audio_out.aac
-vn is no video.
-acodec copy says use the same audio stream that's already in there.

ffmpeg -i video.mp4 will give you the audio/video format info for your file.
Read the output to see what codec it is, to set the right filename extension.

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[Info] ffmpeg encoding

Post#6 by Rava » 23 Jan 2021, 20:43

donald wrote:
23 Jan 2021, 19:46
Hi Rava....may I?
always :)
donald wrote:
23 Jan 2021, 19:46
ffmpeg -i video.mp4 will give you the audio/video format info for your file.
Read the output to see what codec it is, to set the right filename extension.
I should put that into the part of the audio copy out area, sometimes my *.help scripts are too sparse.
donald wrote:
23 Jan 2021, 19:46
-vn is no video.
but… would it still copy subtitles or metadata?
Cheers!
Yours Rava

User avatar
M. Eerie
Moderator
Moderator
Posts: 620
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

[Info] ffmpeg encoding

Post#7 by M. Eerie » 24 Jan 2021, 09:02

Rava wrote:
23 Jan 2021, 20:43
would it still copy subtitles or metadata
You can always map the intended audio stream:

Code: Select all

ffmpeg -i IF -map 0:a:x OF
where x is the audio stream number starting from 0.

See stream specifiers.
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[Info] ffmpeg encoding

Post#8 by Rava » 24 Jan 2021, 17:26

^ thanks M. Eerie and donald, my way still works - and I need to update help.ffmpeg to include name.opus

Determining what audio we have:

Code: Select all

guest@porteus:/mnt/sdc2/sound/mp4$ ffmpeg -i Bodo\ Wartke\ -\ Nicht\ in\ meinem\ Namen\ \(Studio\)-1hBVqgxA_Cg.mkv 
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 10.2.0 (GCC)
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/-4.3.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-libaom --enable-version3 --enable-avresample --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libbluray --enable-libcdio --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-libwavpack --enable-pthreads --enable-postproc --enable-swscale --enable-avfilter --enable-libopus --enable-libspeex --enable-openal --enable-libass --enable-libx264 --enable-libx265 --enable-libgsm --enable-libxvid --enable-libtwolame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libgsm --enable-librtmp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, matroska,webm, from 'Bodo Wartke - Nicht in meinem Namen (Studio)-1hBVqgxA_Cg.mkv':
  Metadata:
    COMPATIBLE_BRANDS: iso6avc1mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    ENCODER         : Lavf58.29.100
  Duration: 00:04:20.02, start: -0.007000, bitrate: 539 kb/s
    Stream #0:0: Video: h264 (Main), yuv420p(tv, bt709, progressive), 854x480 [SAR 1:1 DAR 427:240], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:04:20.000000000
    Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:04:20.021000000
the one line of interest being

Code: Select all

    Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Now using my approach of extracting the audio

Code: Select all

ffmpeg -i Bodo\ Wartke\ -\ Nicht\ in\ meinem\ Namen\ \(Studio\)-1hBVqgxA_Cg.mkv  -codec copy Bodo\ Wartke\ -\ Nicht\ in\ meinem\ Namen\ \(Studio\).opus
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 10.2.0 (GCC)
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/-4.3.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-libaom --enable-version3 --enable-avresample --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libbluray --enable-libcdio --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-libwavpack --enable-pthreads --enable-postproc --enable-swscale --enable-avfilter --enable-libopus --enable-libspeex --enable-openal --enable-libass --enable-libx264 --enable-libx265 --enable-libgsm --enable-libxvid --enable-libtwolame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libgsm --enable-librtmp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, matroska,webm, from 'Bodo Wartke - Nicht in meinem Namen (Studio)-1hBVqgxA_Cg.mkv':
  Metadata:
    COMPATIBLE_BRANDS: iso6avc1mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    ENCODER         : Lavf58.29.100
  Duration: 00:04:20.02, start: -0.007000, bitrate: 539 kb/s
    Stream #0:0: Video: h264 (Main), yuv420p(tv, bt709, progressive), 854x480 [SAR 1:1 DAR 427:240], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:04:20.000000000
    Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:04:20.021000000
Output #0, opus, to 'Bodo Wartke - Nicht in meinem Namen (Studio).opus':
  Metadata:
    COMPATIBLE_BRANDS: iso6avc1mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    encoder         : Lavf58.45.100
    Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:04:20.021000000
      COMPATIBLE_BRANDS: iso6avc1mp41
      MAJOR_BRAND     : dash
      MINOR_VERSION   : 0
      encoder         : Lavf58.45.100
Stream mapping:
  Stream #0:1 -> #0:0 (copy)
Press [q] to stop, [?] for help
size=    4475kB time=00:04:20.00 bitrate= 141.0kbits/s speed= 312x    
video:0kB audio:4442kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.731012%
and testing the result via file

Code: Select all

guest@porteus:/mnt/sdc2/sound/mp4$ file Bodo\ Wartke\ -\ Nicht\ in\ meinem\ Namen\ \(Studio\).opus 
Bodo Wartke - Nicht in meinem Namen (Studio).opus: Ogg data, Opus audio,
guest@porteus:/mnt/sdc2/sound/mp4$ l Bodo\ Wartke\ -\ Nicht\ in\ meinem\ Namen\ \(Studio\).opus
-rwxrwxrwx 1 guest 4582289 2021-01-24 18:10 Bodo Wartke - Nicht in meinem Namen (Studio).opus
and via ffplay

Code: Select all

guest@porteus:/mnt/sdc2/sound/mp4$ ffplay -autoexit -hide_banner -i Bodo\ Wartke\ -\ Nicht\ in\ meinem\ Namen\ \(Studio\).opus
Input #0, ogg, from 'Bodo Wartke - Nicht in meinem Namen (Studio).opus':
  Duration: 00:04:20.03, start: 0.007500, bitrate: 140 kb/s
    Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp
    Metadata:
      DURATION        : 00:04:20.021000000
      COMPATIBLE_BRANDS: iso6avc1mp41
      MAJOR_BRAND     : dash
      MINOR_VERSION   : 0
      encoder         : Lavf58.45.100
  14.51 M-A: -0.000 fd=   0 aq=   23KB vq=    0KB sq=    0B f=0/0    
my approach seems to work fine - when there is only one audio stream. :)
Maybe I should write a reminder about that, might forget about such detail in some months not having extracted any audio.
:beer:

And I will add the

Code: Select all

ffmpeg -i IF -map 0:a:x OF
x is the audio stream number starting from 0
approach in case of several audio streams. :good:
Cheers!
Yours Rava

beny
Full of knowledge
Full of knowledge
Posts: 2086
Joined: 02 Jan 2011, 11:33
Location: italy

[Info] ffmpeg encoding

Post#9 by beny » 24 Jan 2021, 21:31

hi ,i have used this for a work on vob when you have merged all vob in one:

ffmpeg \
-analyzeduration 5000M -probesize 1000M \
-i luci2-3.vob \
-map 0:1 -map 0:2 -map 0:3 -map 0:5 \
-metadata:s:a:0 language=ita -metadata:s:a:0 title="Italian stereo" \
-metadata:s:a:1 language=eng -metadata:s:a:1 title="English stereo" \
-metadata:s:s:0 language=ita -metadata:s:s:0 title="Italian" \
-metadata:s:s:1 language=eng -metadata:s:s:1 title="English" \
-codec:v libx264 -crf 21 \
-codec:a libmp3lame -qscale:a 2 \
-codec:s copy \
luci2-3.mkv
and work the sub and audio stream are in my language but you have all the audio stream listed,and you can choose yours.

beny
Full of knowledge
Full of knowledge
Posts: 2086
Joined: 02 Jan 2011, 11:33
Location: italy

[Info] ffmpeg encoding

Post#10 by beny » 24 Jan 2021, 21:38

sorry but the A is a i don't know why the form is formatting in this way:-metadata:s:a:0 language=ita -metadata:s:a:0 title="Italian stereo" \ this is the right way, again is automatic...

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[Info] ffmpeg encoding

Post#11 by Rava » 24 Jan 2021, 22:11

beny wrote:
24 Jan 2021, 21:38
sorry but the A is a i don't know why the form is formatting in this way:-metadata:s:a:0 language=ita -metadata:s:a:0 title="Italian stereo" \ this is the right way, again is automatic...
this is strange indeed, your source reads -metadata:s: a :0 :crazy:

(I only managed the above via the bbcode trick of inserting space formatted as [ size=1 ] - quote my post to see how its done)
Cave! When using such [ size=1 ] bbcode hacks simply copying and pasting text will get you a whitespace at areas that can cause syntax errors.

Anyway, the -analyzeduration 5000M -probesize 1000M are often needed that large cause some subtitles start only late into *.vob or *.ts (when trying to shrink a digital tv recording)
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[Info] ffmpeg encoding

Post#12 by Rava » 27 Jan 2021, 21:22

beny, I did a 180 seconds test run one minute in using a merged vob and your code.
One without yadif is your code with the streams details changed for my vob.
The one with yadif has added -vf yadif after the IF and prior the OF.
yadif won by smaller filesize:

Code: Select all

44610894 2021-01-26 14:06 Macbeth_V0.mkv
51699019 2021-01-26 14:22 Macbeth_V1_without-yadif.mkv
Cheers!
Yours Rava

User avatar
M. Eerie
Moderator
Moderator
Posts: 620
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

[Info] ffmpeg encoding

Post#13 by M. Eerie » 28 Jan 2021, 13:51

Rava wrote:
27 Jan 2021, 21:22
yadif won by smaller filesize
Yadif (Yet another de-interlace filter) is not related to the filesize of the final video. It's just used to... deinterlace :roll: video footage (normally used in camcorders an some live TV shows).

Interlacing (as opposed to progressive scan) is a common technique used to increase the perceived frame rate, but being a "smart" trick, it leads to undesired artifacts. Is something similar to the mobiles cameras digital zoom vs. (pure) optical zoom (and this is why lenses are expensive).

If you aim for reduced size, using codec x265 over x264 will do it. Or, if you need x264 because of compatibility, you would achieve this by reducing the output bitrate (from both video and audio) or by allowing ffmpeg to do a 2 pass enconding...
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[Info] ffmpeg encoding

Post#14 by Rava » 28 Jan 2021, 19:33

M. Eerie wrote:
28 Jan 2021, 13:51
Yadif (Yet another de-interlace filter) is not related to the filesize of the final video.
The Yadif was the only difference - what else can cause 86% filesize compared to the non-yadiv test run.
Also, some DVDs also use interlacing.

Would I use

Code: Select all

codec:v libx265 -crf 21
or a different -crf parameter when using x265?
Cheers!
Yours Rava

User avatar
M. Eerie
Moderator
Moderator
Posts: 620
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

[Info] ffmpeg encoding

Post#15 by M. Eerie » 29 Jan 2021, 08:56

Rava wrote:
28 Jan 2021, 19:33
what else can cause 86% filesize
Probably the -crf (constant rate factor) parameter. You can play with various values in both x264 and x265. Typical values I commonly see range from 20 (slow more compression), to 26 (faster fewer compression). It also accepts decimal values.

Note that a 3 minute arbitrary cutoff does not necessarily characterize the compression ratio of an entire video. It would take multiple scenes (cuts) with enough contrast/motion to get an approximate value.

A guide to crf
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

Post Reply