From e70ea811c99c16f3382c08153eda573df0825536 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 18 Oct 2018 19:19:56 +0200 Subject: Implement support for .avi files, via ffmpeg - This commit introduces optional dependencies (namely ffmpeg): mat2 will spit a warning when trying to process an .avi file if ffmpeg isn't installed. - Since metadata are obtained via exiftool, this commit also refactors a bit our exfitool wrapper. --- libmat2/video.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libmat2/video.py (limited to 'libmat2/video.py') diff --git a/libmat2/video.py b/libmat2/video.py new file mode 100644 index 0000000..b9f3687 --- /dev/null +++ b/libmat2/video.py @@ -0,0 +1,58 @@ +import os +import subprocess + +from . import exiftool + + +class AVIParser(exiftool.ExiftoolParser): + mimetypes = {'video/x-msvideo', } + meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', 'Directory', + 'FileSize', 'FileModifyDate', 'FileAccessDate', + 'FileInodeChangeDate', 'FilePermissions', 'FileType', + 'FileTypeExtension', 'MIMEType', 'FrameRate', 'MaxDataRate', + 'FrameCount', 'StreamCount', 'StreamType', 'VideoCodec', + 'VideoFrameRate', 'VideoFrameCount', 'Quality', + 'SampleSize', 'BMPVersion', 'ImageWidth', 'ImageHeight', + 'Planes', 'BitDepth', 'Compression', 'ImageLength', + 'PixelsPerMeterX', 'PixelsPerMeterY', 'NumColors', + 'NumImportantColors', 'NumColors', 'NumImportantColors', + 'RedMask', 'GreenMask', 'BlueMask', 'AlphaMask', + 'ColorSpace', 'AudioCodec', 'AudioCodecRate', + 'AudioSampleCount', 'AudioSampleCount', + 'AudioSampleRate', 'Encoding', 'NumChannels', + 'SampleRate', 'AvgBytesPerSec', 'BitsPerSample', + 'Duration', 'ImageSize', 'Megapixels'} + + def remove_all(self) -> bool: + """ + TODO: handle problematic filenames starting with `-` and `--`, + check exiftool.py + """ + cmd = [_get_ffmpeg_path(), + '-i', self.filename, # input file + '-y', # overwrite existing output file + '-loglevel', 'panic', # Don't show log + '-hide_banner', # hide the banner + '-codec', 'copy', # don't decode anything, just copy (speed!) + '-map_metadata', '-1', # remove supperficial metadata + '-map_chapters', '-1', # remove chapters + '-fflags', '+bitexact', # don't add any metadata + '-flags:v', '+bitexact', # don't add any metadata + '-flags:a', '+bitexact', # don't add any metadata + self.output_filename] + + try: + subprocess.check_call(cmd) + except subprocess.CalledProcessError: # pragma: no cover + return False + + return True + + +def _get_ffmpeg_path() -> str: # pragma: no cover + ffmpeg_path = '/usr/bin/ffmpeg' + if os.path.isfile(ffmpeg_path): + if os.access(ffmpeg_path, os.X_OK): + return ffmpeg_path + + raise RuntimeError("Unable to find ffmpeg") -- cgit v1.3