Skip to content

Instantly share code, notes, and snippets.

@mayconvm
Forked from HerculanoGripp/Video Crop for Video Wall
Created September 20, 2016 16:50
Show Gist options
  • Save mayconvm/9d3501cf91492b234d2b74a76e6c11f7 to your computer and use it in GitHub Desktop.
Save mayconvm/9d3501cf91492b234d2b74a76e6c11f7 to your computer and use it in GitHub Desktop.
Script to crop videos for video wall

Python script to crop video for video wall aplications using ffmpeg.

Required:

  • ffmpeg installed;
  • ffprobe installed;

Using: python video-wall.py video-file nxn

  • video-file - path to video file
  • nxn - matriz of video wall

Ex.: python video-wall.py videoToCrop.mp4 2x3

import sys, re, os.path, os, json
patternVideoWall = re.compile("\d*[x]\d*");
args = sys.argv
videoFile = args[1];
matrizVideoWall = args[2];
if not patternVideoWall.match(matrizVideoWall):
print "Error video wall parameters. Ex. python video-wall.py videoFile.mp4 2x2";
sys.exit();
if not os.path.exists(videoFile):
print "Error video file doesn't exists";
sys.exit();
matrizVideoWall = matrizVideoWall.split('x');
rows = int(matrizVideoWall[0]);
columns = int(matrizVideoWall[1]);
cmdGetSettings = "ffprobe -v error -show_entries stream=width,height \-of default=noprint_wrappers=1 " + videoFile;
videoFileSettings = os.popen(cmdGetSettings).read().split("\n");
width = int(videoFileSettings[0].replace("width=",""));
height = int(videoFileSettings[1].replace("height=",""));
widthPart = width/columns;
heightPart = height/rows;
print widthPart;
print heightPart;
for row in range(1, rows + 1):
print "Row " + str(row);
for column in range(1, columns+1):
xIni = str((column - 1) * widthPart);
yIni = str((row - 1) * heightPart);
cmdCropVideo = "ffmpeg -i " + videoFile + " -filter:v crop="+ str(widthPart) + ":" + str(heightPart) + ":" + xIni + ":" + yIni + " out_" + str(row) + "_" + str(column) + ".mp4 -y";
print cmdCropVideo;
result = os.popen(cmdCropVideo).read();
print result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment