How to re-encode H.264 video with minimal quality loss

ffmpegh.264handbrakevideo-encoding

I have a lot of MPEG-TS files (.TS container but H.264 video) and playback is fine except that when you skip forward/backward or fast forward it's very sluggish and gets pixelated, etc.

I've been trying to do research and I'm guessing that they were encoding with very few reference blocks (ie. it's a capture from a DVB-S satellite stream).

When I re-encode them with Handbrake (.MP4 container) they play very, very good and seeking in the video is instant, etc, etc.

Is it possible to transcode/re-encode my MPEG-TS files with minimal quality loss? If so, what is my best bet? They are each about 2 Mbps (ie. 2 GB per hour) but I don't want to re-encode them if "minimal quality loss" requires 10+ GB per file. I'm hoping to keep the video are the same size.

Can anybody give me any advice?

Best Answer

It's possibly a container problem, copying it to a new container format with

avconv -i input.ts -c copy output.mp4

may well fix your problem. This will be 100% lossless. If that doesn't work, a crf of 18 is normally considered 'visually lossless'; you can set this is HandBrake (under the 'video' tab), or with avconv:

avconv -i input.ts -c:a copy -c:v libx264 -crf 18 -preset veryfast output.mp4

The presets are: ultrafast, superfast, veryfast, faster, fast, medium, slower, slow, veryslow. The slower the preset, the smaller the file (but with an increase in encode time). In my personal experimentation, I've found the biggest dropoff in terms of filesize is between superfast and veryfast, after that it seems to be much more incremental.

NOTE: If you want, ffmpeg can do everything avconv can do, with identical syntax (simply replace all instances of avconv with ffmpeg).

FOOTNOTE: You may find this ffmpeg x264 encoding guide useful for more information.

Related Question