Convert2MKV - New Options; New Hardware Logic! - WIP [UPDATED 27/01/19]

Convert2MKV

The Updates Continue :smiley:

It’s been a few months without an update. I’ve made some great strides in the logic and options.

What’s New?

  • New Option “vbitrate” - You can now use “auto” which will set a default of 1 Byte per pixel, or you can override the logic with a specific number like you would in ffmpeg (using the standard options of K, M or G eg. “10M”)
  • New Option “hw” - Now you can choose either “cpu” or “vaapi”, meaning you can now use the script even if you don’t have hardware acceleration available
  • New Option “overwrite” - Instead of deleting your original files, you can now choose to create a backup instead. The original file will be renamed *.bak. If you are satisfied that this script is capable of carrying out the tasks you want it to, you can choose “yes” to replace the original file
  • New Logic “bitrate” - Using the height and width of input video, the bitrate is calculated per pixel using a simple height x width formula. This can be overridden as listed above
  • New parameters - x265 parameters have been added and can be manually tweaked if needed
  • New Logic “transcode” - using the “hw” option, the logic determines how to use the output, either with vaapi or cpu transcoding
  • New Logic for file moves - when encoding the file, the output is to the $tempdir using the original file name (making it easier to find if needed). It is then moved after transcoding is complete, taking into account the “overwrite” parameter
  • Removed Logic h264 - The script no longer outputs in h264 as this codec is already easily accessible via other tools, so is wasted effort in this script
  • Removed Option - No longer extracts to RGB, difference was negligible, and added extra transcoding time

Currently, the script is capable of:

  • Accepting almost any format supported by FFMPEG
  • Automatically determining the input video and audio codec
  • Making automatic choices for output, based on the input
  • Altering its output method to best suit the input combination of video and audio streams
  • Outputting to an MKV or MP4 container
  • HEVC Video encoding
  • Transcoding to AAC Audio
  • Detect and convert Rext profile videos
  • AAC & AC3 Audio Passthrough
  • Parsing intire libraries, including those without escaped characters
  • Replacing the file(s) after transcode, or creating backups
  • Re-mapping audio of *.ogv files during transcoding
  • Adjusting FPS during transcode
  • Adjusting Preset and CRF
  • Manually selecting Audio Sample Rate during transcode of non AAC or AC3 audio
  • Preliminary QuickSync transcoding support for hevc

Future Plans

  • Add option to download subtitles
  • Add option to have a different output path
  • Open to suggestions :slight_smile:

Get The Script Here!

Conclusion

Adding more options and better logic has been difficult for me (non programmer that I am), but I’m slowly getting better at it. The more I work on it, the more I understand how to do it better. I’ll continue to work on it as I have the time. I’ve been enjoying seeing the progress :slight_smile:

I’ll update again as soon as I can!

Cheers!

Saved For Future Posts

To save the amount of processing time you’re expending there, there is a much faster way if you only wish to change the container. Using this, you can now add your desired conversion parameters if you want more than only a container change.

#!/bin/tcsh

  foreach i (*.avi *.m4v *.mpg *.mp4)
    ffmpeg -i "$i" -c:v copy -c:a copy "${i:r}.mkv"
  end

@ChuckPa said:
To save the amount of processing time you’re expending there, there is a much faster way if you only wish to change the container. Using this, you can now add your desired conversion parameters if you want more than only a container change.

That’s very cool :slight_smile:

Thanks for the contribution!

Glad I could help.

@ChuckPa said:
Glad I could help.

Out of curiosity, I noticed you’ve scripted for ffmpeg, are you using CentOS?

-EDIT- Just noticed tcsh, so maybe Unix/*BSD?

Updated Bump

Or even: ffmpeg -i “$i” -c copy “${i:r}.mkv”

Includes the first sub (param -c:s) … works on win … dunno about Linux :slight_smile:

Sorry for the delay. I was away on vacation and away from plex.

tcsh runs in linux and bsd. I’m running fedora (redhat)

Yes -c copy includes everything.

https://www.freebsd.org/cgi/man.cgi?tcsh(1)

@mm98 said:
-c copy

Yup, that’d work. I just wanted it expanded so it kept in line with all the other options. That way you can still encode/transcode either video or audio with just a minor adjustment to the script. It’s just more logically laid out. You’re free to shorten it for your own use of course :slight_smile:

@ChuckPa said:
tcsh runs in linux and bsd. I’m running fedora (redhat)
https://www.freebsd.org/cgi/man.cgi?tcsh(1)

Thank you :slight_smile:

How does this script deal with multiple audio tracks and multiple subtitles?

@peva said:
How does this script deal with multiple audio tracks and multiple subtitles?

it’s not the script which deals with this. The script simply runs ffmpeg for each of the files it finds. The -c copy option for ffmpeg copies all audio, video, and subtitles, as they are. This is where you are free to make any changes and perform actual conversions if you wish.

Many thanks!

Might need to try this on FreeBSD. Thanks

To show what ffmpeg can do, give this a read. It’s an absolutely amazing tool.

http://ffmpeg.org/documentation.html

@peva said:
How does this script deal with multiple audio tracks and multiple subtitles?

Like ChuckPa said, it assumes that there is only a single video track, and then converts all audio contained in the file to AAC to match the h264 of the MKV container. I chose h264 and AAC as they are the most compatible with my devices, but you can adjust it to any codec that suits you.

You could use libxvid and mp3 if you want use and AVI container :slight_smile:

@robot2xl said:
Many thanks!

You’re most welcome!

@sremick said:
Might need to try this on FreeBSD. Thanks

Awesome, let me know how you go! I’m not sure avconv is available on BSD, but you could rather easily adjust the script to use ffmpeg (avconv is a fork of ffmpeg. it contains more up to date codec support and the latest bug fixes)

@ChuckPa said:
To show what ffmpeg can do, give this a read. It’s an absolutely amazing tool.

Documentation

It truly is. I used to use ffmpeg, once avconv was supported by most of my apps, I switched. But the differences are negligible. Almost any script that can run on ffmpeg, can be converted to avconv and vice versa.

@ntrevena said:
It truly is. I used to use ffmpeg, once avconv was supported by most of my apps, I switched. But the differences are negligible. Almost any script that can run on ffmpeg, can be converted to avconv and vice versa.

avconv and ffmpeg are forks of the same base. I forget the reasons for the fork but I’m sure you can find it out.

To all: Remember, the -c copy refers to the copy codec which simply takes input “AS-IS” and copies to the ouput. The output file handler puts it new container format. The long form of this command, which makes the next step(s) more obvious is:

ffmpeg -i filename.old_extension -c:v copy -c:a copy -c:s copy output_filename.new_extension

This expressly specifies “video copy, audio copy, and subtitle copy”

With this as the long form, it’s now easy to see how the video, audio, and subtitle tracks can be manipulated. To aid with files which have multiple tracks of audio/video/subtitles, the -map option allows for appropriate selection.

Enjoy :smiley:

edit: I think substituting the ffmpeg command with avconv will do the same operation without altering any of the command line parameters

@ChuckPa said:
avconv and ffmpeg are forks of the same base. I forget the reasons for the fork but I’m sure you can find it out.

Yup. If I remember rightly, Canonical threw their weight behind libav. The team behind libav were originally core developers of ffmpeg. From what I recall, the devs who left to start libav, had a disagreement with the other devs about the future of ffmpeg. Some believed features were more important, others that the API and stability was the future. Those behind the API and stability, were those who left to start libav.

@ChuckPa said:
ffmpeg -i filename.old_extension -c:v copy -c:a copy -c:s copy output_filename.new_extension

There is a small issue here, it doesn’t take into account the incompatibilities between codec and container. For example, if you choose an input file of h264 mp4, and an output of 3gp, then it would corrupt the output file. The input codec has to be transcoded to h263. for it to work with the 3gp container. Does that make sense?

@ChuckPa said:
To aid with files which have multiple tracks of audio/video/subtitles, the -map option allows for appropriate selection.

Very cool, didn’t think about subtitles. I’ll add that in the next iteration of the script. :slight_smile:

@ChuckPa said:
edit: I think substituting the ffmpeg command with avconv will do the same operation without altering any of the command line parameters

Should do, though avconv can be a little particular. Test and see! :smiley:

Good catch on the h263. Thanks! Yes, that is a codec change and requires handling. My simple command assumed only container change (avi - mkv - mp4)

Yes, I do remember that talk. I thought it was Canonical that got in there and I seem to remember passions ran deep.

Please do test and see. It will tell us how far they’ve difted apart.

Bump for update