FFMPEG convert .mpg video to .mp4 without lose quality

18,002

You're encoding but you're not setting any encoding parameters. Read this guide and then choose the appropriate parameters for your use case, e.g.:

ffmpeg -i $in -c:v libx264 -c:a libfaac -crf 20 -preset:v veryslow $out

Do not use -r. If you want better quality, use a lower -crf value. If you want smaller files, use a higher -crf value. If you care more about bitrate than quality targets, see the appropriate section in the aforementioned guide.

Share:
18,002
reterius
Author by

reterius

Updated on June 11, 2022

Comments

  • reterius
    reterius almost 2 years

    How can i convert video .mpg to .mp4 wihtout love quality. My codes below:

     $INCPATH       = dirname(__FILE__).'/';
    
     $id = 99999;
     $ext = "MPG" ;
    
      $input_dir = $INCPATH."../img/videos/".$id.".".$ext ;   
      $output_dir2 = $INCPATH."../img/videos/".$id.".mp4" ;
    
        $q = '/usr/bin/ffmpeg -i '.$input_dir.' -c:v libx264 -c:a libfaac -r     30 '.$output_dir2." 2>&1" ;
    
        $out = shell_exec($q) ;
        $input_dir  = $output_dir2 ;
    
         echo var_dump($out);
    

    Response that:

                string(5735) "ffmpeg version N-76539-g480bad7-syslint Copyright (c) 2000-2015 the FFmpeg developers
                  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-16)
                  configuration: --prefix=/usr/local/cpffmpeg --enable-shared --enable-nonfree --enable-gpl --enable-pthreads --enable-libopencore-amrnb --enable-decoder=liba52 --enable-libopencore-amrwb --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --extra-cflags=-I/usr/local/cpffmpeg/include/ --extra-ldflags=-L/usr/local/cpffmpeg/lib --enable-version3 --extra-version=syslint
                  libavutil      55.  5.100 / 55.  5.100
                  libavcodec     57. 15.100 / 57. 15.100
                  libavformat    57. 14.100 / 57. 14.100
                  libavdevice    57.  0.100 / 57.  0.100
                  libavfilter     6. 15.100 /  6. 15.100
                  libswscale      4.  0.100 /  4.  0.100
                  libswresample   2.  0.101 /  2.  0.101
                  libpostproc    54.  0.100 / 54.  0.100
                Input #0, mpeg, from '/home/imcajans/public_html/kzmk/../img/videos/99999.MPG':
                  Duration: 00:00:08.16, start: 0.116600, bitrate: 2955 kb/s
                    Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 720x576 [SAR 64:45 DAR 16:9], max. 9100 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
                    Stream #0:1[0x80]: Audio: ac3, 48000 Hz, stereo, fltp, 256 kb/s
                [libx264 @ 0x161e820] using SAR=64/45
                [libx264 @ 0x161e820] using cpu capabilities: none!
                [libx264 @ 0x161e820] profile High, level 3.1
                [libx264 @ 0x161e820] 264 - core 148 r2638 7599210 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=18 lookahead_threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
                Output #0, mp4, to '/home/imcajans/public_html/kzmk/../img/videos/99999.mp4':
                  Metadata:
                    encoder         : Lavf57.14.100
                    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 720x576 [SAR 64:45 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc
                    Metadata:
                      encoder         : Lavc57.15.100 libx264
                    Stream #0:1: Audio: aac (libfaac) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, s16, 128 kb/s
                    Metadata:
                      encoder         : Lavc57.15.100 libfaac
                Stream mapping:
                  Stream #0:0 -> #0:0 (mpeg2video (native) -> h264 (libx264))
                  Stream #0:1 -> #0:1 (ac3 (native) -> aac (libfaac))
                Press [q] to stop, [?] for help
                frame=   48 fps=0.0 q=0.0 size=       0kB time=00:00:01.19 bitrate=   0.3kbits/s dup=8 drop=0    
                frame=   66 fps= 58 q=29.0 size=      20kB time=00:00:01.83 bitrate=  87.2kbits/s dup=11 drop=0    
                frame=   77 fps= 43 q=29.0 size=      51kB time=00:00:02.17 bitrate= 190.3kbits/s dup=13 drop=0    
                frame=   89 fps= 38 q=29.0 size=      86kB time=00:00:02.60 bitrate= 272.2kbits/s dup=15 drop=0    
                frame=  101 fps= 34 q=29.0 size=     124kB time=00:00:02.98 bitrate= 340.6kbits/s dup=17 drop=0    
                frame=  114 fps= 32 q=29.0 size=     166kB time=00:00:03.39 bitrate= 400.3kbits/s dup=19 drop=0    
                frame=  126 fps= 31 q=29.0 size=     201kB time=00:00:03.84 bitrate= 428.3kbits/s dup=21 drop=0    
                frame=  137 fps= 30 q=29.0 size=     236kB time=00:00:04.20 bitrate= 459.6kbits/s dup=23 drop=0    
                frame=  151 fps= 29 q=29.0 size=     275kB time=00:00:04.65 bitrate= 484.2kbits/s dup=25 drop=0    
                frame=  165 fps= 29 q=29.0 size=     316kB time=00:00:05.12 bitrate= 505.4kbits/s dup=27 drop=0    
                frame=  177 fps= 28 q=29.0 size=     350kB time=00:00:05.50 bitrate= 521.3kbits/s dup=29 drop=0    
                frame=  188 fps= 27 q=29.0 size=     387kB time=00:00:05.93 bitrate= 534.6kbits/s dup=31 drop=0    
                frame=  198 fps= 27 q=29.0 size=     419kB time=00:00:06.25 bitrate= 548.7kbits/s dup=33 drop=0    
                frame=  212 fps= 27 q=29.0 size=     461kB time=00:00:06.72 bitrate= 562.4kbits/s dup=35 drop=0    
                frame=  223 fps= 26 q=29.0 size=     495kB time=00:00:07.04 bitrate= 576.6kbits/s dup=37 drop=0    
                frame=  240 fps= 26 q=29.0 size=     574kB time=00:00:07.59 bitrate= 618.9kbits/s dup=40 drop=0    
                frame=  245 fps= 20 q=-1.0 Lsize=     834kB time=00:00:08.17 bitrate= 835.9kbits/s dup=41 drop=0    
                video:696kB audio:128kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.134396%
                [libx264 @ 0x161e820] frame I:2     Avg QP:22.05  size: 10777
                [libx264 @ 0x161e820] frame P:187   Avg QP:22.67  size:  3323
                [libx264 @ 0x161e820] frame B:56    Avg QP:23.98  size:  1242
                [libx264 @ 0x161e820] consecutive B-frames: 54.7% 44.1%  1.2%  0.0%
                [libx264 @ 0x161e820] mb I  I16..4: 33.5% 63.9%  2.5%
                [libx264 @ 0x161e820] mb P  I16..4:  1.9%  1.8%  0.0%  P16..4: 46.9%  4.7%  6.4%  0.0%  0.0%    skip:38.3%
                [libx264 @ 0x161e820] mb B  I16..4:  0.1%  0.1%  0.0%  B16..8: 34.4%  0.5%  0.0%  direct: 3.1%  skip:61.8%  L0:41.1% L1:57.8% BI: 1.1%
                [libx264 @ 0x161e820] 8x8 transform intra:51.1% inter:96.1%
                [libx264 @ 0x161e820] coded y,uvDC,uvAC intra: 25.1% 86.2% 30.4% inter: 9.5% 38.5% 1.1%
                [libx264 @ 0x161e820] i16 v,h,dc,p: 41%  5% 11% 43%
                [libx264 @ 0x161e820] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36%  6% 48%  1%  1%  3%  1%  3%  1%
                [libx264 @ 0x161e820] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 54% 10% 15%  3%  3%  8%  2%  5%  2%
                [libx264 @ 0x161e820] i8c dc,h,v,p: 66% 12% 18%  3%
                [libx264 @ 0x161e820] Weighted P-Frames: Y:0.0% UV:0.0%
                [libx264 @ 0x161e820] ref P L0: 66.7%  5.8% 17.5% 10.0%
                [libx264 @ 0x161e820] ref B L0: 81.1% 18.9%  0.0%
                [libx264 @ 0x161e820] ref B L1: 99.5%  0.5%
                [libx264 @ 0x161e820] kb/s:697.87
                "
    

    And my video which has .mpg extention its best quality: https://imcajans.com/img/videos/99999.mpg

    And my video which coverted from .mpg to .mp4 its bad quality: https://imcajans.com/img/videos/99999.mp4

  • Ronald S. Bultje
    Ronald S. Bultje over 8 years
    Like I said, decrease the -crf value. Try -crf 10. If that's still not good enough, try even lower. You can go all the way to -crf 0, which is lossless. The file size will increase.
  • llogan
    llogan about 6 years
    This answer doesn't address the question.
  • Ravi Ganesh
    Ravi Ganesh about 6 years
    -vcodec libx264 without any parameters worked best for me.
  • Ravi Ganesh
    Ravi Ganesh about 6 years
    "-crf 20 -preset:v veryslow" was very slow. I would recommend FOR /F "tokens=*" %G IN ('dir /b *.MPG') DO bin\ffmpeg -i "%G" -vcodec libx264 -acodec aac -af "volume=20dB" "%~nG.mp4" for stereo MPEG. I found no loss of video quality and it was fast.
  • Grégory
    Grégory over 3 years
    libfaac is removed from ffmpeg since 2016 use -c:a aac instead