avatar
2 minutes read

ffmpeg not opencv

The mysterious, somewhat barely documented, library of OpenCV.

Having gotten one's feet wet performing object detection on batch images with OpenCV, I thought I would try some very rudimentary motion detection.

Aside from a few hickups, here and there, everything went pretty smooth. Then I decided to make life difficult again, and incorporate ffmpeg into the script. To do this I used the ffmpeg-python library. I had used it before to probe information from videos, but this time I was going to "really use it", and crop the video before processing it with opencv.

Then everything went to hell, and I kept searching the opencv documentation for answers to the error that were being put out, finally I went back to basics, and started using log to troubleshoot the issue. Low and behold, opencv was not the problem, ffmpeg was. As it turns out, setting up the output is not enough to tell ffmpeg to run. You have to specifically use run(). The syntax looks as in below:

def crop(vid, log):
    log.info('Setting Stream...')
    stream = ffmpeg.input(vid)
    log.info('Cropping Stream...')
    audio = stream.audio.filter('anull')
    video = stream.video.crop(width=850, height=500, x=0, y=0)
    output_file = 'cropped.mp4'
    log.info('Writing Stream...')
    out = ffmpeg.output(audio, video, output_file)
    log.info('Running Stream...')
    out.run()
    return output_file

Pressed for time, so I will have to leave it there. Sorry, this wasn't more on topic.

0 Comment