FFMPEG Fixes Your Video Files

Posted at

ffmpeg-logo

FFMPEG's stream copy is a mode that is really helpful to solve video glitches, slowness due to bad video-index, etc..

In that mode, ffmpeg omits the decoding and encoding step for the specified stream, so it does only demuxing and muxing (its very fast). It is useful for changing the container format or modifying container-level metadata. The diagram above will, in this case, simplify to this:
 _______              ______________            ________
|       |            |              |          |        |
| input |  demuxer   | encoded data |  muxer   | output |
| file  | ---------> | packets      | -------> | file   |
|_______|            |______________|          |________|

 

Since there is no decoding or encoding, it is very fast (few seconds for demuxing and muxing for full size 2 hours video), and there is no quality loss. However, it might not work in some cases because of many factors. Applying filters is obviously also impossible (since filters work on uncompressed data).

ffmpeg -threads 4 -i vid.avi -vcodec copy -acodec copy vidout.avi


the -threads 4 is optional, but recommended.





FFMPEG is free software licensed under the LGPL and you can download 'already build' binary files, ready to be used, at Zeranoe FFmpeg builds

this site is great since it builds FFMPEG with many useful external libs (and encoders) the current version of FFMPEG available for download has the following external libs compiled with ("boundle"): bzip2, Fontconfig, FreeType, Frei0r, Game Music Emu, GnuTLS, GSM, iLBC, LAME, libass, libbluray, libbs2b, libcaca, libebur128, libiconv, libmfx, libsoxr, Modplug-XMMS, OpenCORE AMR, OpenH264, OpenJPEG, Opus, Package, RTMPDump, Schroedinger, Snappy, Speex, Theora, TwoLAME, vid.stab, VisualOn AMR-WB, Vorbis, vpx, WavPack, WebP, x264, x265, XAVS, Xvid, XZ Utils, z.lib, zlib

this means the following formats are supported in the version of FFMPEG you'll be downloading,
(if you've got x64 bit OS, download the x64 bit version..)


You can also download a pre-compiled version with many external libs from here (x64) or here (x32)






September 15th, 2016- Edit:
Video get stuck on playing, either locally or streaming? messed up colors? other weird stuff?? 👀︎

You need an EXTRA FIXING POWER! 💪︎💪︎

The following command line will cure 💉︎ your video file (anytype to mp4)

ffmpeg -y -hide_banner -i "input.mp4" -bt 50M -vf "mpdecimate,setpts=N/FRAME_RATE/TB" -preset veryslow -crf 21 -tune zerolatency -pix_fmt yuv420p -movflags "+faststart" "output.mp4"




  • ☞︎ Cures double frames, without damaging the video frame-rate or length (bug in Some HD Samsung phones).


  • Fixes 🔧︎ video/audio sync problems ⌚︎, if needed.


  • Will 🌠︎ load much faster, both locally and on online streaming websites.


  • YouTube will process your video faster ⚡︎.


  • Your video will play smoothly 🚀︎ on all players and mobile devices.


  • Un-mess-up videos, that are shown with messed-up colors ☜︎ (mostly green'ish and pink, on Windows Media-Player).


    • It is well known that WMP, incorrectly interprets H.264's yuvj420p as yuv420p
      If you'll try to convert from/to a wmv-file you could probably see the following warning:
      No pixel format specified, yuvj420p for H.264 encoding chosen.
      Use -pix_fmt yuv420p for compatibility with outdated media players.
      





  • Will take some time to run but end-result will be minimal in size and maximum quality, as much as possible.


  • Read more








September 24th, 2016- Edit #1:
Make your video compatible with ALL mobile devices
Add :

  1. -profile:v "baseline"

  2. -level "3.0"


Without it, Android 4, Apple TV below 2.0, iPhone 3 and iPod touch 1st generation will not support playback of your video.

so the final command is:
ffmpeg -y -hide_banner -i "input.mp4" -profile:v "baseline" -level "3.0" -bt 50M -vf "mpdecimate,setpts=N/FRAME_RATE/TB" -preset veryslow -crf 21 -tune zerolatency -movflags "+faststart" -pix_fmt yuv420p "output.mp4"







September 24th, 2016- Edit #2:
Make FFMPEG to parse correctly the frames-per-second from videos you've filmed using either Samsung's UHD or Canon 1080i AVHCD, set for 60 FPS.
Add:


  1. -r 60 before the -i.
    • It overrides FFMPEG's input's frame-rate-detection.



  2. -r 60 right after the -i.
    • To set the output's frame-rate.



so the final command is:
ffmpeg -y -hide_banner -r 60 -i "input.mp4" -r 60 -profile:v "baseline" -level "3.0" -bt 50M -vf "mpdecimate,setpts=N/FRAME_RATE/TB" -preset veryslow -crf 21 -tune zerolatency -movflags "+faststart" -pix_fmt yuv420p "output.mp4"







October 24th, 2016: edit.
This is a batch script I've fiddled with, constructing a high-end mp4 file, with very little latency, perfect for streaming. it also "tries" to very gently fix bad indexing, double-frames and other common bugs.

in order to use it one must download the latest nightly binary build of ffmpeg from https://ffmpeg.zeranoe.com/builds/, and copy&paste the following into a new file, save it with the name video_fixer.cmd







November 3rd, 2016 - edit:
this CMD batch file is smaller, with less quality lost due using default framing attributes (GOP,I-frames),
and also support drag&drop of AVI files which do not support the -pix_fmt "yuv420p" with Option pixel_format not found

@echo off
set FILE_INPUT="%~s1"
set FILE_OUTPUT="%~d1%~p1%~n1_fixed.mp4"

::--------------------------------------------------------------------------------
call ffmpeg.exe -y -hide_banner -fflags "+genpts" ^
-pix_fmt "yuv420p" ^
-i %FILE_INPUT% ^
-ss "00:00:00.001" ^
-filter:v "yadif=0:-1:0,mpdecimate,setpts=N/FRAME_RATE/TB" ^
-preset "veryslow" -crf "21" ^
-profile:v "baseline" -level "3.0" -tune "zerolatency" -movflags "+faststart" ^
%FILE_OUTPUT%
::--------------------------------------------------------------------------------


::--------------------------------------------------------------------------------
if %errorlevel%==0 goto DONE_SUCCESS;
::--------------------------------------------------------------------------------


::-------------------------------------------------------------------------------- error ("Option pixel_format not found." ??) , try again with no pix_fmt.
call ffmpeg.exe -y -hide_banner -fflags "+genpts" ^
-i %FILE_INPUT% ^
-ss "00:00:00.001" ^
-filter:v "yadif=0:-1:0,mpdecimate,setpts=N/FRAME_RATE/TB" ^
-preset "veryslow" -crf "21" ^
-profile:v "baseline" -level "3.0" -tune "zerolatency" -movflags "+faststart" ^
%FILE_OUTPUT%
::--------------------------------------------------------------------------------


::--------------------------------------------------------------------------------
:DONE_SUCCESS

pause









November 3rd (#2) - edit:
this variation will provide support for an infinite list of arguments (and will process them one by one, which you may replicate to use two, or more, in parallel...).
..Plus, the IF/ELSE structure although more logical and stream-lined, converted to jumpy GOTO, since it works better in CMD for some reason... ;)

@echo off

:LOOP
::-------------------------- has argument ?
if ["%~1"]==[""] (
echo done.
goto END;
)
      ::-------------------------- argument exist ?
if not exist %~s1 (
echo not exist
goto NEXT;
)

echo exist
if exist %~s1\\NUL (
echo is a directory
goto NEXT;
)

echo is a file
set FILE_INPUT="%~s1"
set FILE_OUTPUT="%~d1%~p1%~n1_fixed.mp4"
::--------------------------------------------------------------------------------
call ffmpeg.exe -y -hide_banner -fflags "+genpts" ^
-pix_fmt "yuv420p" ^
-i %FILE_INPUT% ^
-ss "00:00:00.001" ^
-filter:v "yadif=0:-1:0,mpdecimate,setpts=N/FRAME_RATE/TB" ^
-preset "veryslow" -crf "21" ^
-profile:v "baseline" -level "3.0" -tune "zerolatency" -movflags "+faststart" ^
%FILE_OUTPUT%
::--------------------------------------------------------------------------------
if %errorlevel%==0 (
goto NEXT;
)
::-------------------------------------------------------------------------------- error ("Option pixel_format not found." ??) , try again with no pix_fmt.
echo trying again without [-pix_fmt "yuv420p"]
call ffmpeg.exe -y -hide_banner -fflags "+genpts" ^
-i %FILE_INPUT% ^
-ss "00:00:00.001" ^
-filter:v "yadif=0:-1:0,mpdecimate,setpts=N/FRAME_RATE/TB" ^
-preset "veryslow" -crf "21" ^
-profile:v "baseline" -level "3.0" -tune "zerolatency" -movflags "+faststart" ^
%FILE_OUTPUT%
::--------------------------------------------------------------------------------

:NEXT
shift
goto LOOP


:END

pause




Nov13,2016:
@echo off
:LOOP
::has argument ?
if ["%~1"]==[""] (
echo done.
goto END:
)
::argument exist ?
if not exist %~s1 (
echo not exist
goto NEXT:
)
::file exist ?
echo exist
if exist %~s1\\NUL (
echo is a directory
goto NEXT:
)
::OK
echo is a file

set FILE_INPUT="%~s1"
set FILE_OUTPUT="%~d1%~p1%~n1_fixed.mkv"

call ffmpeg.exe -y -threads 16 ^
-hide_banner -loglevel "info" -stats ^
-i %FILE_INPUT% -ss "00:00:00.001" ^
-flags "-loop+naq+low_delay" ^
-fflags "+genpts+discardcorrupt+fastseek" ^
-movflags "+faststart" ^
-pix_fmt "yuv420p" ^
-tune "zerolatency" ^
-profile:v "baseline" -level "3.0" ^
-af "aresample=async=1:min_hard_comp=0.100000" ^
-vf "yadif=0:-1:0,mpdecimate,setpts=N/FRAME_RATE/TB-STARTPTS,format=pix_fmts=yuv420p" ^
-preset "ultrafast" -crf "23" ^
%FILE_OUTPUT%

:NEXT
shift
goto LOOP

:END
pause





Nov15,2016

@echo off
:LOOP
::has argument ?
if ["%~1"]==[""] (
echo done.
goto END;
)
::argument exist ?
if not exist %~s1 (
echo not exist
goto NEXT;
)
::file exist ?
echo exist
if exist %~s1\\NUL (
echo is a directory
goto NEXT;
)
::OK
echo is a file

set TARGET_FPS=25

set FILE_INPUT="%~s1"
set FILE_OUTPUT="%~d1%~p1%~n1_fixed.mkv"

call ffmpeg.exe -y ^
-hide_banner -loglevel "info" -stats ^
-i %FILE_INPUT% ^
-threads 16 ^
-flags "-loop+naq+low_delay" ^
-flags2 "+fast+ignorecrop+showall+export_mvs" ^
-fflags "+genpts+discardcorrupt+fastseek" ^
-movflags "+faststart" ^
-tune "zerolatency" ^
-pix_fmt "yuv420p" ^
-profile:v "high" -level "5.0" ^
^
-x264opts "keyint=4" -g "2" -forced-idr "true" -refs "16" -rc-lookahead "16" ^
-preset "veryslow" -crf "23" -subq "9" -qcomp "0.60" ^
^
-ss "00:00:00.001" ^
-af "aresample=async=1:min_hard_comp=0.100000" -vsync "1" ^
-vf "yadif=0:-1:0,dejudder=cycle=20,mpdecimate,fps=fps=%TARGET_FPS%,setpts=N/FRAME_RATE/TB-STARTPTS,format=pix_fmts=yuv420p" ^
-map_metadata "-1" ^
%FILE_OUTPUT%

:NEXT
shift
goto LOOP

:END
pause

the following can reach encoding-speeds of x6.5(++)
due to using profile high level 5.0 and undocumented speed hacks (mostly extra buffers in RAM: -flags2 "+fast...")


  • audio sync transformed to the newer form: -af "aresample=async=1:min_hard_comp=0.100000" without the start=0 thing (:first_pts=0)which is buggy.. especially if you trimmmmmm the start.


  • using -subq "9" for a slightly better video-quality


  • using yadif=0:-1:0 to deinterlace, dejudder=cycle=20 to remove interlace leftovers from all the ranges of interlace data available mpdecimate to remove similar frames fps=fps=%TARGET_FPS% (use 25/30/60) which is needed to prevent too much "speeded-up" video bug, the setpts=N/FRAME_RATE/TB-STARTPTS filter corrects the presentation time stamp (PTS) considering the starting point, format=pix_fmts=yuv420p (in additional to -pix_fmt "yuv420p" flag) actually converts whatever format there is to the compatible yuv420p.


  • -ss "00:00:00.001" along with the -fflags "...-fastseek" is a trick of my own invention to help "cure" stubborn files, by pretending to dump the first frame, using the encoding process in a "hard-mode"..
    note that if you'll use +fastseek you'll gain a lot of speed (x5 to x6.5+) but the frame cut of the first image will be somewhat inaccurate..


  • -profile:v "high" -level "5.0" is substantially increasing the encoding time, but you might require a compatible video player (such as MX-Player, for Android). You may switch it to -profile:v "baseline" -level "3.0" if you require a more.. compatible (?) encoding. the high profile also allows reading


  • -map_metadata "-1" helps you to remove all meta-data, you may remove this switch, especially if you are converting video taken on a phone, which includes the screen-state to play the video-in (for example "portrait" or "landscape"). You can easily edit the meta-data (idv3 just like mp3) using the MP3Tag program (even include a thumbnail).







The following command will simply use the demuxer without encoding,
it is very similar to the original "fix all" command (first one in this article),
but it also has support for every stream included, without being specific to audio/video/subs...
and it supports drag&drop, of infinite-length list of files,
@echo off
:LOOP
::has argument ?
if ["%~1"]==[""] (
  echo done.
  goto END;
)
::argument exist ?
if not exist %~s1 (
  echo not exist
  goto NEXT;
)
::file exist ?
echo exist
if exist %~s1\\NUL (
  echo is a directory
  goto NEXT;
)
::OK
echo is a file

set FILE_INPUT="%~s1"
set FILE_OUTPUT="%~d1%~p1%~n1_fixed%~x1"

call ffmpeg -y -hide_banner -loglevel "info" -stats -threads 8 -i %FILE_INPUT% -codec copy %FILE_OUTPUT%

echo.
echo.

:NEXT
shift
goto LOOP

:END
pause







July 9th, 2017 update:
ignoring corrupted parts, arguments are added to variable - which mean no more use of '^',
and allowing comments (and reducing whitespace), organised commands in groups,
identified a one command that factor the quality for deblocking, which was a bit overkill in
the versions before (ffmpeg default) - decreased its default value from 8 to 1, added some blur fixing, removed '-pix_fmt' in favor of ',format=pix_fmts=yuv420p' video-filter, explicitly placed at the end of the filter list (and explicitly after all of the deinterlace and frame-setting fixes). made sure to use open_gop higher chroma level, used 'b_strategy' with I-frames (and to disable adaptive number of B-frames), forbidding unaligned content and negative time values and demand accurate calculating of several values from the video stream instead of filling it as a data input from the video data...

you can still send unlimited amount of files too...

@echo off
:LOOP
::has argument ?
if ["%~1"]==[""] (
  echo done.
  goto END;
)
::argument exist ?
if not exist %~s1 (
  echo not exist
  goto NEXT;
)
::file exist ?
echo exist
if exist %~s1\\NUL (
  echo is a directory
  goto NEXT;
)
::OK
echo is a file

::--------------------------------------------------------------------------------

set FILE_INPUT="%~s1"
set FILE_OUTPUT="%~d1%~p1%~n1_fixed.mkv"
set ARGS=

::program switches
set ARGS=%ARGS%  -y  -hide_banner  -loglevel "info"  -stats  -threads "16"  -strict "experimental"

::input
set ARGS=%ARGS% -i %FILE_INPUT%

::flags
set ARGS=%ARGS% -flags              "+naq+low_delay+global_header-unaligned-ilme-cgop-loop-output_corrupt"
set ARGS=%ARGS% -flags2             "+fast+ignorecrop+showall+export_mvs"
set ARGS=%ARGS% -fflags             "+ignidx+genpts+nofillin+discardcorrupt-fastseek"
set ARGS=%ARGS% -movflags           "+faststart+disable_chpl"
set ARGS=%ARGS% -avoid_negative_ts  "make_zero"
set ARGS=%ARGS% -tune               "zerolatency"
set ARGS=%ARGS% -profile:v          "high"
set ARGS=%ARGS% -level              "5.2"
::set ARGS=%ARGS% -profile:v  "high" -level "5.0"
::set ARGS=%ARGS% -pix_fmt            "yuv420p"

::motion-estimation
set ARGS=%ARGS% -subq      "9"
set ARGS=%ARGS% -refs      "25"
set ARGS=%ARGS% -me_method "full"

::frames logic (I,IDR)
set ARGS=%ARGS% -g               "2"
set ARGS=%ARGS% -intra-refresh   "0"
set ARGS=%ARGS% -forced-idr      "1"
::                                          use I-frames (disable adaptive number of B-frames)
set ARGS=%ARGS% -b_strategy      "0"
set ARGS=%ARGS% -x264opts        "keyint=4"
::                                          max B frames between reference frames
set ARGS=%ARGS% -bf              "2"
::                                          increase number of lookahead fragments
set ARGS=%ARGS% -lookahead_count "4"


::frames processing (mostly for WebM)
::set ARGS=%ARGS% -lag-in-frames  "25"
::set ARGS=%ARGS% -frame-parallel "1"
::set ARGS=%ARGS% -tile-columns   "6"
::set ARGS=%ARGS% -tile-rows      "2"
::set ARGS=%ARGS% -auto-alt-ref   "1"

::start/finish
set ARGS=%ARGS% -ss "00:00:00.001"
set ARGS=%ARGS% -to "00:00:30.000"

::meta data processing
set ARGS=%ARGS% -map_chapters  "-1"
set ARGS=%ARGS% -map_metadata  "-1"

::video processing
set ARGS=%ARGS% -aq-mode      "variance"
::                            (default 8 or -1 ??)   Reduces blocking and blurring in flat and textured areas. (from -1 to FLT_MAX) (default -1)
set ARGS=%ARGS% -aq-strength  "1"
::                            (default -1)           Reduce fluctuations in QP (before curve compression)
set ARGS=%ARGS% -cplxblur     "10"

set ARGS=%ARGS% -crf          "23"
set ARGS=%ARGS% -preset       "veryslow"
set ARGS=%ARGS% -qcomp        "0.60"
set ARGS=%ARGS% -rc-lookahead "25"
::              align, deinterlace, fix chroma, adjust frames to 16:9 by padding, accurate frame-rate and set video-step-speed according to the frame-rate.
set ARGS=%ARGS% -vf "setpts=PTS-STARTPTS,yadif=0:-1:0,dejudder=cycle=20,mpdecimate,pad=width=ih*16/9:height=ih:x=(ow-iw)/2:y=(oh-ih)/2:color=#00000000,fps=fps=25,setpts=N/FRAME_RATE/TB-STARTPTS,format=pix_fmts=yuv420p"

::audio processing
set ARGS=%ARGS% -af "asetpts=PTS-STARTPTS,aresample=async=1:min_hard_comp=0.100000" -vsync "1"

call ffmpeg.exe %ARGS% %FILE_OUTPUT%

echo.

::--------------------------------------------------------------------------------

:NEXT
shift
goto LOOP

:END
pause

::-mpv_flags                "+strict_gop"
::-force_duplicated_matrix  "1"
::-strict_gop               "1"