Editing MPC video with shaders
There is a task: To change the video “on the fly” during playback - to swap the right and left parts. Not to reflect, but to change, i.e. cut the picture into two parts and swap them. You can, of course, do this using a frameserver like AviSynth , but this is not quite “on the fly” - you need to write a script for each video file. I want to do it quickly and without straining.
On a fig? To make a course of lectures on machine learning from Yandex more convenient to view. The lecturer points to the presentation points live, and one has to constantly skip over the entire screen with a glance in order to understand what it is about:
Use the shader tool available in Media Player Classic . In the standard package there are several ready-made shaders for image editing - “Emboss”, “Grayscale”, “16-235 to 0-255” and the like. We don't need what's needed, so we are creating a new shader (a small program in the HLSL language ):
Save it to the \ Shaders folder of the media player (for example, C: \ Program Files \ MPC-HC \ Shaders) under the name Shift.hlsl . Next, go to the Media Player Classic settings ( Options / Playback / Shaders ) and add a new Shift shader to the Active pre-Resize shaders list .
This video processing method does not load the CPU, since all the work is done by the graphics card, in any case, if it is real and not emulated.
This is a pixel shader that should output a color of the point depending on the coordinates that are transmitted through the parameter tex . The variable s0 contains the texture with the current frame of the video.
The new horizontal coordinate is calculated as the remainder of the modulo 1 division of the sum of the current coordinate and the swapLine shift . Thus, we shift the point to the right and rearrange it to the left side when it goes beyond the dimensions.
Finally, the command tex2D (s0, tex) returns the color of the point with the desired coordinate from the source frame.
On a fig? To make a course of lectures on machine learning from Yandex more convenient to view. The lecturer points to the presentation points live, and one has to constantly skip over the entire screen with a glance in order to understand what it is about:
Decision
Use the shader tool available in Media Player Classic . In the standard package there are several ready-made shaders for image editing - “Emboss”, “Grayscale”, “16-235 to 0-255” and the like. We don't need what's needed, so we are creating a new shader (a small program in the HLSL language ):
sampler s0 : register(s0);
float4 main(float2 tex : TEXCOORD0) : COLOR
{
// swapLine 0.5 = in the middlefloat swapLine = 1082.0 / 1920.0;
tex.x = (tex.x + swapLine) % 1.0;
float4 c0 = tex2D(s0, tex);
return c0;
}
Save it to the \ Shaders folder of the media player (for example, C: \ Program Files \ MPC-HC \ Shaders) under the name Shift.hlsl . Next, go to the Media Player Classic settings ( Options / Playback / Shaders ) and add a new Shift shader to the Active pre-Resize shaders list .
This video processing method does not load the CPU, since all the work is done by the graphics card, in any case, if it is real and not emulated.
What is it, Barimore?
This is a pixel shader that should output a color of the point depending on the coordinates that are transmitted through the parameter tex . The variable s0 contains the texture with the current frame of the video.
The new horizontal coordinate is calculated as the remainder of the modulo 1 division of the sum of the current coordinate and the swapLine shift . Thus, we shift the point to the right and rearrange it to the left side when it goes beyond the dimensions.
Finally, the command tex2D (s0, tex) returns the color of the point with the desired coordinate from the source frame.
PS
- Cool article on shaders, here, on Habré: "Creating shaders . "
- A good selection of MPC shaders is at github.com/dinfinity/mpc-pixel-shaders