Reputation: 187
I want to use python subprocess to call ffmpeg and use crop detect to find all black in a video. The crop detect return I want to put into a string variable and put in a database. At the moment I can get the process running in terminal and but I am unsure about how to grab the specific part of the terminal (stdout) output:
the script:
def cropDetect():
p = subprocess.Popen(["ffmpeg", "-i", "/Desktop/ffprobe_instance/Crop_detect/video_file.mpg", "-vf", "cropdetect=24:16:0", "-vframes", "10", "dummy.mp4"], stdout=subprocess.PIPE)
result = p.communicate()[0]
print result
# SCRIPT
cropDetect()
Result in terminal: [Parsed_cropdetect_0 @ 0x7fa1d840cb80] x1:719 x2:0 y1:575 y2:0 w:-704 h:-560 x:714 y:570 pos:432142 pts:44102 t:0.490022 crop=-704:-560:714:570
How do I take "crop=-704:-560:714:570" and put it into a variable that I can store in a database?
As per update:
def cropDetect1():
p = subprocess.check_output(["ffmpeg", "-i", "/Desktop/ffprobe_instance/Crop_detect/video_file.mpg", "-vf", "cropdetect=24:16:0", "-vframes", "10", "dummy.mp4"])
match = re.search("crop\S+", p)
crop_result = None
if match is not None:
crop_result = match.group()
print "hello %s" % crop_result
I can't seem to print out the "crop_result" - I am presuming that means that the variable is empty?
UPDATE: Found it:
def detectCropFile(localPath):
fpath = "/xxx/xx/Desktop/Crop_detect/videos/USUV.mp4"
print "File to detect crop: %s " % fpath
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
infos = p.stderr.read()
print infos
allCrops = re.findall(CROP_DETECT_LINE + ".*", infos)
print allCrops
mostCommonCrop = Counter(allCrops).most_common(1)
print "most common crop: %s" % mostCommonCrop
print mostCommonCrop[0][0]
global crop
crop = mostCommonCrop[0][0]
video_rename()
Use: p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
to pipe it out
Upvotes: 0
Views: 1595
Reputation: 187
Ok found it
def detectCropFile(localPath):
fpath = "/xxx/xx/Desktop/Crop_detect/videos/USUV.mp4"
print "File to detect crop: %s " % fpath
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
infos = p.stderr.read()
print infos
allCrops = re.findall(CROP_DETECT_LINE + ".*", infos)
print allCrops
mostCommonCrop = Counter(allCrops).most_common(1)
print "most common crop: %s" % mostCommonCrop
print mostCommonCrop[0][0]
global crop
crop = mostCommonCrop[0][0]
video_rename()
So use this in the subprocess call to grab the values from crop detect:
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Upvotes: 0
Reputation: 6596
It looks like you don't need to be using the lower level subprocess.Popen
interface; I'd just call subprocess.check_output
, which will return the value of the os call as a string. From there, just do string processing to get your value.
result = subprocess.check_output(["ffmpeg", "-i" ... ])
# this regex matches the string crop followed by one or more non-whitespace characters
match = re.search("crop\S+", result)
crop_result = None
if match is not None:
crop_result = match.group()
If ffmpeg output is instead to stderr:
result = subprocess.check_output(["ffmpeg", ...], stderr=subprocess.STDOUT)
Upvotes: 1