Ever since DeoVR released the specs of alpha_pack videos, I'd been wondering if I could use ffmpeg to achieve the same result.
It was a long time overdue, but I finally get around to make a windows batch script to do this. I downloaded an example video with alpha.
The original video is just a simple fireball.
I used ffmpeg to generate a pseudo 3D video. Here left and right are exactly the same.
The script will first create a temporary file "output_temp.mov", which look like this. The size is quite small, because it's only 40% the size of original video and contains only red channel.
Then the script will combine original video and output_temp.mov to generate the final result, which look like this.
OK, without further ado, here is the script:
@echo off
if "%~1"=="" goto error
echo This script will create a temporary mask file output_temp.mov,
echo then combine %~nx1 and output_temp.mov to create alpha packed file %~n1_alpha%~x1
echo If not ready then hit Ctrl-Break to stop, otherwise,
pause
:: Here we set all the environment variables for the ffmpeg to use.
for /f "delims=" %%a in ('ffprobe -hide_banner -show_streams %1 2^>nul ^| findstr "^width= ^height= ^duration="') do set "v_%%a"
:: Get basic video properties
:: ==============
echo original video width=%v_width%
echo original video height=%v_height%
echo original video duration=%v_duration%
:: Get calculated variables
:: ==============
set /A w_out="%v_width%*4/10"
set /A h_out="%v_height%*4/10"
set /A h_w_out="%w_out%/2"
set /A h_h_out="%h_out%/2"
set /A q_w_out="%w_out%/4"
set /A q_h_out="%h_out%/4"
set /A right2_x="%w_out%-%q_w_out%"
set /A x_topleft="(%v_width%/2)-(%h_w_out%/2)"
set /A y_bottomleft="%v_height%-%h_h_out%"
echo x topleft =%x_topleft%
echo y bottomleft =%y_bottomleft%
set /A x2_topleft="%v_width%-%q_w_out%"
set /A y2_topleft="%v_height%-%h_h_out%"
:: Create a temporary output_temp.mov file for alpha mask.
:: You can extract your own alpha mask video here and resize it to 40% by your own.
:: The result file should be small compared to the original.
ffmpeg -y -i %1 ^
-f lavfi -i "color=gray:size=%w_out%x%h_out%:duration=%v_duration%" ^
-f lavfi -i "color=black:size=%w_out%x%h_out%:duration=%v_duration%" ^
-f lavfi -i "color=white:size=%w_out%x%h_out%:duration=%v_duration%" ^
-filter_complex "[0:v]alphaextract,scale=w=%w_out%:h=%h_out%[alpha];[alpha][1:v][2:v][3:v]threshold,colorchannelmixer=0:0:1:0:0:0:0:0:0:0:0" ^
output_temp.mov
echo If the temporary file is successfully created, hit any key to continue, otherwise hit Ctrl-Break to quit now.
pause
ffmpeg -y -i %1 -i output_temp.mov -filter_complex "[1:v]colorkey=color=black:similarity=0.1,split=6[ovr1][ovr2][ovr3][ovr4][ovr5][ovr6];[ovr1]crop=x=0:y=0:w=%h_w_out%:h=%h_h_out%[topleft];[ovr2]crop=x=0:y=%h_h_out%:w=%h_w_out%:h=%h_h_out%[bottomleft];[ovr3]crop=x=%h_w_out%:y=0:w=%q_w_out%:h=%h_h_out%[1right];[ovr4]crop=x=%right2_x%:y=0:w=%q_w_out%:h=%h_h_out%[2right];[ovr5]crop=x=%h_w_out%:y=%h_h_out%:w=%h_w_out%:h=%h_h_out%[3right];[ovr6]crop=x=%right2_x%:y=%h_h_out%:w=%q_w_out%:h=%h_h_out%[4right];[0:v][topleft]overlay=x=%x_topleft%:y=%y_bottomleft%[out1];[out1][bottomleft]overlay=x=%x_topleft%:y=0[out2];[out2][1right]overlay=x=%x2_topleft%:y=%y2_topleft%[out3];[out3][2right]overlay=x=0:y=%y_bottomleft%[out4];[out4][3right]overlay=x=%x2_topleft%:y=0[out5];[out5][4right]overlay=x=0:y=0" %~n1_alpha%~x1
pause
exit
:error
echo The input parameter is empty.
echo You need to drag a file to this batch script to run it.
pause
You just need to copy and paste everything from above and save it as a batch file named "alpha_packing.cmd"
Then you just drag your video file with alpha channel to this script. It should be straightforward.
Of course you need to install ffmpeg before using it.
The original video should better have a resolution that's multiple of 10, like 3840x2160
Also it's better to put the script and original video file in the same folder.
Let me know if this script has a problem. Thank you !
PS: It's ffmpeg, so you can use your own hardware encoder to make the process faster, or get a higher quality result.
PS2: Right now any alpha value more than 50% will be included. To adjust the mask to include lower alpha values, you can change the threshold setting "color=gray" (which equals to 0x808080) to something like "color=0x202020".
PS3: If for some reason the script is not working, you can download this script directly and try again.