Possession of a large video library today is not uncommon, and usually it is trying to collect everything in its best quality. However, the other side of the coin is incompatibility with old household players, ancient laptops and other portable gadgets.
I ran into this for the banal reason of everyday comfort.
Having a good media station under Windows, with a connected 60 "plasma and six-channel sound, tuned with soul, with written scripts for
AutoHotkey and rewritten for my own USB
WinLIRC IR receiver, I realized that I couldn’t watch downloaded movies every evening, since I don’t want to do it getting out from under the blankets in the bedroom. Where hangs a simple 40 "combine, bought at a sale, on the occasion, in the store of one of the major retail chains. The possibilities of a domestic player embedded in an LCD-TV turned out to be enough for reading flash drives and SD cards of any size, but in formats it turned out to be limited to AVI and MPEG-4-ASP codecs and weaker.
The solution was simple - to overtake a couple of hundreds of unsigned movies in AVI, and then automatically create a Lite-version when the download of the torrent rtorrent is finished on the home server. Thus, the solution had to work both under Windows and under Linux, since the server is running Gentoo.
')
However, it was not so simple to technically implement all this with a swoop. Windows converters with blackjack and other tinsel were swept away immediately, without attempting to install, and the rest remained only
Avidemux , having both a cross-platform implementation and a normal CLI interface. It only remained to automate the process and give the technology to do what it was supposed to do - to work.
Avidemux scripting automation
In actual fact, the CLI Avidemux interface turned out to be quite flawed, but it is saved by the ability to write the configuration of the project to the script and then run it with the run option:
avidemux2_cli --run script
It’s quite simple to peek at the format of the script, all you have to do is launch the Avidemux GUI version, make all the necessary settings, and then save the project to a file. This file contains all the settings for codecs and conversion filters.
Actually changing the container using Avidemux can be done without transcoding the video, but in my case it became necessary to change the resolution of the output file and limit its width to 720 pixels. However, I did not find another way to set the output resolution, except for explicitly specifying the width and height in pixels, so the idea of ​​using one script for all files disappeared. I had to use
MediaInfo , which is also available for Linux, and get dimensions from it, and most importantly the aspect displayed on the screen, and calculate the height based on the width set. Additionally, the idea was to slightly stretch the “widescreen” films by height in order to compensate for the visual compression of the TV hanging at a low angle.
Thus, the awk-script appeared (the choice of awk is determined by the compactness of its
implementation for windows, as compared to pearl, for example). The script searches the files in the * .mkv mask (the search string is easy to change for any platform), then calls MediaInfo for each found file and creates a script for AviDemux, and also adds a command to invoke the converter in the batch file. In the future, this script is easily reworked to convert a selected file and connect it to the download manager.
Script source
In the above source, the settings of the selected video codec have been deleted, since they take up more space than the entire script and can be easily obtained from the AviDemux project. In this case, all double quotes should be escaped with a backslash character (in this awk is similar to C).
BEGIN { # # , WIDTH = 720 HEIGHT = 404 # CROP = 1 # . / STRETCH = 0.15 # , BATCH = "run.cmd" # AVIDEMUX = "P:\\myProgs\\AVIDemux.2.5.6\\avidemux2_cli.exe" MEDIAINFO = "P:\\myProgs\\MediaInfo_CLI_0.7.59\\MediaInfo.exe" # , (dir, ls, find) LIST = "dir /b *.mkv" # ASPECT = WIDTH / HEIGHT STRETCH += 1 while((LIST | getline fn) > 0) { printf("FILE: %s\r\n", fn); InfoCommand = MEDIAINFO " --Output=Video;%Width%/%Height%/%AspectRatio%/%DisplayAspectRatio% \""fn"\"" if((InfoCommand | getline tmp) > 0) { if (match(tmp, /([0-9]+)\/([0-9]+)\/([0-9.]+)\/([0-9.]+)/, m)) { w = int(m[1]) h = int(m[2]) a = 0.0 + m[3] d = 0.0 + m[4] if (a != d) { printf("\tAspect/Display are not equal %d %d\r\n", a, d); } # correct dimentions to square pixels cw = w ch = w / d # stretch to fit display if (cw / ch > ASPECT) { sh = cw / d * STRETCH printf("\timage is wider than display, do vertical stretch\r\n") if (sh > cw / ASPECT) { sh = cw / ASPECT } sw = cw # crop to fit display if (CROP && (sw / sh > ASPECT)) { printf("\timage is still wider than display, do horisontal crop\r\n") sw = sh * ASPECT } fw = (sw < WIDTH) ? rint(sw, 8) : rint(WIDTH, 8) fh = rint(sh / sw * fw, 4) } else if (cw / ch < ASPECT) { sw = ch * d * STRETCH printf("\timage is higher than display, do horisontal stretch\r\n") if (sw > ch * ASPECT) { sw = ch * ASPECT } sh = ch # crop if (CROP && (sw / sh > ASPECT)) { printf("\timage is still higher than display, do horisontal crop\r\n") sh = sw / ASPECT } fh = (sh < HEIGHT) ? rint(sh, 4) : rint(HEIGHT, 4) fw = rint(sw / sh * fh, 8) } rw = cw - sw rh = ch - sh printf("\t%dx%d -stretch-crop-> %d(%+d)x%d(%+d) -display-fit-> %dx%d\r\n", w, h, sw, -rw, sh, -rh, fw, fh); # calc 4 crop values if (rw < 0) rw = 0 if (rh < 0) rh = 0 rwl = int(rw / 2) rwr = int(rw - rwl) rht = int(rh / 2) rhb = int(rh - rht) # make new filenames rsfn = fn avifn = fn sub(/\.[A-Za-z0-9_]+$/, ".rs", rsfn) if (rsfn == fn) rsfn = rsfn ".rs" sub(/\.[A-Za-z0-9_]+$/, ".avi", avifn) if (avifn == fn) avifn = avifn ".avi" # avidemux sript does not like "\" in path gsub(/\\/, "/", fn) gsub(/\\/, "/", avifn) # save run script printf("//AD <- Needed to identify//\r\n")>rsfn printf("var app = new Avidemux();\r\napp.load(\"%s\");\r\napp.video.setPostProc(3,3,0);\r\napp.video.addFilter(\"crop\",\"left=%d\",\"right=%d\",\"top=%d\",\"bottom=%d\");\r\napp.video.addFilter(\"resize\",\"w=%d\",\"h=%d\",\"algo=0\");\r\n", fn, rwl, rwr, rht, rhb, fw, fh)>rsfn printf("app.video.codecPlugin(\"****\");\r\n")>rsfn printf("app.audio.reset();\r\napp.audio.codec(\"Lame\",128,20,\"80 00 00 00 00 00 00 00 02 00 00 00 05 00 00 00 00 00 00 00 \");\r\napp.audio.normalizeMode=1;\r\napp.audio.normalizeValue=0;\r\napp.audio.delay=0;\r\napp.audio.mixer=\"STEREO\";\r\napp.audio.drc=true;\r\napp.setContainer(\"AVI\");\r\n")>rsfn printf("app.save(\"%s\");\r\napp.Exit();\r\n", avifn)>rsfn # add to batch printf("%s --nogui --run \"%s\"\r\n", AVIDEMUX, rsfn)>BATCH } } close(InfoCommand) } close(LIST) } function rint(v, b) { # floor value to specified base return int(int(v + 0.5) / b) * b }