Crop video into a 4x4 grid/tiles/matrix efficiently via command-line ffmpeg?

余生长醉 提交于 2021-01-01 06:37:47

问题


Hello Stackoverflow community!

I dread having to ask questions, but there seems to be no efficient way to take a single input video and apply a matrix transformation/split the video into equal sized pieces, preferably 4x4=16 segments per input.

I tried using all the libraries such as ffmpeg and mencoder, but having 16 outputs can be as slow as 0.15x. The goal of my project is the split the video into 16 segments, rearrange those segments and combine back into a final video; later reversing the process in HTML5 canvas. Here is a picture to help you understand what I am talking about: the source but also the final destination after reorganizing the pieces

after the segments are exported, I will recombine the pieces in a puzzle manner (they are also inverted and rotated but unrelated)

I do not believe you can do this all in one command, so my goal is to crop into 16 mapped outputs quickly, then reassemble them in a different order. But I can do the other parts myself. Ideally there would be a way to move pixel blocks eg 100x100 and just move them around. My math is not strong enough.. I really appreciate the work you guys do!

admin@dr.com


回答1:


Basic template is

ffmpeg -i in.mp4
  -filter_complex
     "[0]split=16[a][b][c][d][e][f][g][h][i][j][k][l][m][n][o][p];
      [a]crop=iw/4:ih/4:0*iw/4:0*ih/4[a];
      [b]crop=iw/4:ih/4:1*iw/4:0*ih/4[b];
      [c]crop=iw/4:ih/4:2*iw/4:0*ih/4[c];
      [d]crop=iw/4:ih/4:3*iw/4:0*ih/4[d];
      [e]crop=iw/4:ih/4:0*iw/4:1*ih/4[e];
      [f]crop=iw/4:ih/4:1*iw/4:1*ih/4[f];
      [g]crop=iw/4:ih/4:2*iw/4:1*ih/4[g];
      [h]crop=iw/4:ih/4:3*iw/4:1*ih/4[h];
      [i]crop=iw/4:ih/4:0*iw/4:2*ih/4[i];
      [j]crop=iw/4:ih/4:1*iw/4:2*ih/4[j];
      [k]crop=iw/4:ih/4:2*iw/4:2*ih/4[k];
      [l]crop=iw/4:ih/4:3*iw/4:2*ih/4[l];
      [m]crop=iw/4:ih/4:0*iw/4:3*ih/4[m];
      [n]crop=iw/4:ih/4:1*iw/4:3*ih/4[n];
      [o]crop=iw/4:ih/4:2*iw/4:3*ih/4[o];
      [p]crop=iw/4:ih/4:3*iw/4:3*ih/4[p];
      [b][d][i][p]hstack=4[w];
      [n][g][j][c]hstack=4[x];
      [e][o][a][l]hstack=4[y];
      [h][f][m][k]hstack=4[z];
      [w][x][y][z]vstack=4"
      out.mp4

Not pretty, but gets the job done. Best if your video dimensions are (mod 4). If not, a scaler may be needed at the end after the vstack. The inputs specified to the hstacks are what control the shuffling of the pieces.

The rearranging part happens pretty fast - speed 7x for a 1080p input. Encoding drags that down to 0.9x. If you output to 720p, that jumps to 1.9x. For a 720p input, kept as is, speed is 2.5x. All benchmarked on a Haswell i5.



来源:https://stackoverflow.com/questions/43550344/crop-video-into-a-4x4-grid-tiles-matrix-efficiently-via-command-line-ffmpeg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!