You are not logged in.

#1 2016-12-29 09:58:58

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Converting videos respecting quality

That is an old problem that I have had for some time but haven't found a satisfying answer. What I want is to convert a video from one format to another respecting the overall quality. I do not want bit for bit quality respecting nor respecting the quality in an absolutely perfect sense but that the converted video is the same overall visual quality. Tool to convert video generally take a lot of options and does not seem to be able to do what I want unless you carefully examine the original format and guess the corresponding options for the output format. Does such a tool even exist?

Offline

#2 2016-12-29 10:04:22

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Offline

#3 2016-12-29 21:50:36

teckk
Member
Registered: 2013-02-21
Posts: 590

Re: Converting videos respecting quality

What I want is to convert a video from one format to another respecting the overall quality

Are you wanting to just change from one container to another?

ffmpeg -i file.avi -c:a copy -c:v copy file.mp4

You may not be able to change from some containers or formats directly to another without re-encoding.
For example, this won't work, aac to mp3

ffmpeg -i file.m4a -vn -c:a copy file.mp3

You'll have to re-encode it.

ffmpeg -i file.m4a -vn -c:a libmp3lame -b:a 128k file.mp3

Look and see what a container supports.

This will work:

ffprobe file.mp4
 ......
 Duration: 00:00:31.95, start: 0.000000, bitrate: 704 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 630 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 72 kb/s (default)
    Metadata:
      creation_time   : 2016-10-14T09:40:45.000000Z
      handler_name    : IsoMedia File Produced by *****
ffmpeg -i file.mp4 -c:a copy -c:v copy file.mkv
ffprobe file.mkv
......
Input #0, matroska,webm, from 'file.mkv':
  Metadata:
    COMPATIBLE_BRANDS: isommp42
    MAJOR_BRAND     : mp42
    MINOR_VERSION   : 0
    ENCODER         : Lavf57.56.100
  Duration: 00:00:31.95, start: 0.000000, bitrate: 705 kb/s
    Stream #0:0: Video: h264 (Constrained Baseline), yuv420p(progressive), 640x360 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)
    Metadata:
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:00:31.898000000
    Stream #0:1: Audio: aac (LC), 44100 Hz, mono, fltp (default)
    Metadata:
      HANDLER_NAME    : IsoMedia File Produced by *****
      DURATION        : 00:00:31.950000000

An atsc television transport stream capture example:

ffprobe tv1.ts
Input #0, mpegts, from 'tv1.ts':
  Duration: 00:00:08.79, start: 60592.199433, bitrate: 11814 kb/s
    Stream #0:0[0x34]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s
    Stream #0:1[0x31]: Video: mpeg2video (Main), yuv420p(tv, bottom first), 1280x720 [SAR 1:1 DAR 16:9], Closed Captions, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc

   

ffmpeg -i tv1.ts -c:a copy -c:v copy tv1.avi
ffprobe tv1.avi
Input #0, avi, from 'tv1.avi':
  Metadata:
    encoder         : Lavf57.56.100
  Duration: 00:00:08.73, start: 0.000000, bitrate: 11577 kb/s
    Stream #0:0: Video: mpeg2video (Main) (mpg2 / 0x3267706D), yuv420p(tv, progressive), 1280x720 [SAR 1:1 DAR 16:9], Closed Captions, 11192 kb/s, 119.88 fps, 59.94 tbr, 119.88 tbn, 119.88 tbc
    Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, 5.1(side), fltp, 384 kb/s

This will also give good quality and take up less space

ffmpeg -i tv1.ts -c:a ac3 -b:a 128k -c:v libx264 -b:v 2000k tv2.avi
ls -hs tv1.ts
13M tv1.ts

ls -hs tv1.avi
13M tv1.avi

ls -hs tv2.avi
2.2M tv2.avi

see man ffmpeg

Offline

#4 2016-12-30 04:58:59

adamlau
Member
Registered: 2009-01-30
Posts: 418

Re: Converting videos respecting quality

Pretty much what teckk said. Extract your required streams (e.g. audio/video/subtitle) and remux them into your preferred container. As mentioned above, you may need to transcode one, or more of the streams into a container compatible format.


Arch Linux + sway
Debian Testing + GNOME/sway
NetBSD 64-bit + Xfce

Offline

#5 2016-12-30 08:29:45

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: Converting videos respecting quality

@teckk If you invest some time and have a knowledge of the input and output format for each conversion, your method will work. But you specify some parameters like bitrates in the output. How to know which one is appropriate? That depends on the output quality and I want it to match the input quality. If the original is a 4K video, I want to keep a 4K video so a high bitrate is needed. If the original is just a SD video, there is no point to having a bitrate so high. Moreover, the bitrate depends on of the compression algorithm. If I want to convert some mp4 video to mp2 to put on a DVD, the bitrate must be higher because the algorithm of the latter format is much less efficient. How do I know which one will match the quality of the original? I was hoping that someone has figured all this out in order to be able to just type something like this:

convert original.mp4 destination.mp2

That would be great in a batch script too. But maybe something like this doesn't exist.

@graysky This auto-reencode script come close to what I want but is very limited. I was hoping something much more general.

Last edited by olive (2016-12-30 08:30:11)

Offline

#6 2016-12-30 16:17:02

teckk
Member
Registered: 2013-02-21
Posts: 590

Re: Converting videos respecting quality

How to know which one is appropriate?

From years of doing it. A 2500k libx264 video is just about indistinguishable from a DVD .vob dump, and about half or less in size. In fact 2500k is overkill but I like the quality.

That depends on the output quality and I want it to match the input quality

Then don't change it. Copy the video from the input to the output. Or if you want it like it is then why change it at all? The reason to change a video is because you don't like something about it, it's file size, it's codec, it's frame size...If all you want is to change containers then you can do that quickly, with the output being about the same size. Quality of output when re-encoding also depends on the codec that you use. A 1200k x264 video will be of better quality than a 1200k mpeg2 video.

If the original is a 4K video, I want to keep a 4K video so a high bitrate is needed.

Then why don't you dump the DVD to .vob, dump the BD to file, or dump the digital tv stream to .ts file and leave it that way. In the case of a transport stream you may want to put the video into a mp4 container so that it has a file header on it. Some video players won't play a .ts file. mplayer, mpv, ffplay will. Or if you like a optical disk the way it is then make an .iso from it with dd or mkisofs.

If I want to convert some mp4 video to mp2 to put on a DVD

This probably won't turn out as good as a normal dvd unless the mp4 was high quality. Even then encoding from something to .mp4, then re-encoding again back to mpeg2 is going to loose quality.

How do I know which one will match the quality of the original?

Don't re-encode it if you want it the same. If you re-encode it, it will change.

I can give you some examples.

ffprobe myvideo.mp4
.....
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'myvideo.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-09-07T09:41:24.000000Z
  Duration: 00:44:47.48, start: 0.000000, bitrate: 633 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 854x480 [SAR 1:1 DAR 427:240], 503 kb/s, 24.87 fps, 24.87 tbr, 24871 tbn, 49.74 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2016-09-07T09:41:41.000000Z
      handler_name    : IsoMedia File Produced by ****
ffmpeg -i myvideo.mp4 -c:a mp2 -b:a 128k -c:v mpeg2video -qscale:v 2 out.mpg
ffprobe out.mpg
......
Input #0, mpeg, from 'out.mpg':
  Duration: 00:01:03.74, start: 0.529089, bitrate: 2847 kb/s
    Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv, progressive), 854x480 [SAR 1:1 DAR 427:240], 25 fps, 25 tbr, 90k tbn, 50 tbc
    Stream #0:1[0x1c0]: Audio: mp2, 44100 Hz, stereo, s16p, 128 kb/s

Why don't you experiment for yourself and see what satisfies your requirements.
Start with a small 1 min video piece.

ffmpeg -i input_video.vob -c:a copy -c:v copy -t 00:01:00 file.vob
ffmpeg -i file.vob -c:a copy -c:v libx264 -b:v 1000k x264_1.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v libx264 -b:v 1200k x264_2.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v libx264 -b:v 1400k x264_3.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v libx264 -b:v 1600k x264_4.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v libx264 -b:v 1800k x264_5.mp4
ffmpeg -i file.vob -c:a copy -c:v mpeg2video -b:v 1000k mpeg2_1.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v mpeg2video -b:v 1500k mpeg2_2.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v mpeg2video -b:v 2000k mpeg2_3.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v mpeg2video -b:v 2500k mpeg2_4.mp4 &&
ffmpeg -i file.vob -c:a copy -c:v mpeg2video -b:v 3000k mpeg2_5.mp4

You can do the same with the audio

ffmpeg -i file.vob -vn -c:a ac3 -b:a 128k out1.m4a &&
ffmpeg -i file.vob -vn -c:a ac3 -b:a 192k out2.m4a &&
ffmpeg -i file.vob -vn -c:a ac3 -b:a 256k out3.m4a &&
ffmpeg -i file.vob -vn -c:a ac3 -b:a 384k out4.m4a &&

It depends on what your goal is. Best quality, then don't re-encode it. Make it fit a format like video DVD, then it will have to be ac3 and mpeg2. Best q for file size, then libx264. Got all kinds of storage space?, then dump the vob to file. Make it fit a data single layer DVD, then it will have to be < 4.3 GB.

Look at ffmpeg docs.
Look at ffmpeg -codecs

Those examples I gave were simple ones with fixed bitrates. Using average bitrates will reduce file sizes.

You won't gain anything by doing a 2 pass with ffmpeg using x264.
You will decrease file size and gain q with mpeg2 with 2 pass.

Look at the docs. If you have a specific question about how ffmpeg does something ..ask.

Last edited by teckk (2016-12-30 16:18:20)

Offline

#7 2016-12-30 16:48:32

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: Converting videos respecting quality

teckk wrote:

How to know which one is appropriate?

From years of doing it.

But that's my problem. About any solution I find needs this (years of doing it...). My original motivation to change the encoding was compatibility with some appliances (like TV). But more and more of these appliances just understand mp4 and your solution to not change anything make more and more sense.

Although not having found exactly what I want; I thank you because your post explains the basic of all the codecs and bitrate conversion in very clear terms.

Offline

#8 2017-01-02 11:39:17

imdn
Member
Registered: 2016-04-12
Posts: 41

Re: Converting videos respecting quality

If you are encoding to H.264, the -crf switch is exactly what you're looking for without needing to specify the bitrate manually. A range between 18-21 should preserve visual quality. (More here - https://trac.ffmpeg.org/wiki/Encode/H.264#crf)

BTW, arch-forums might not be the appropriate place for this discussion. The are places such as forum.videohelp where you will get better suggestions.

Last edited by imdn (2017-01-02 11:41:24)

Offline

Board footer

Powered by FluxBB