forked from ftarlao/check-media-integrity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg_checker.py
28 lines (24 loc) · 1.16 KB
/
ffmpeg_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import subprocess
import json
class FFmpegChecker:
@staticmethod
def check(filename, error_detect='default', threads=0):
ffmpeg_command = ['ffmpeg', '-v', 'error', '-i', filename, '-f', 'null', '-']
if error_detect != 'default':
if error_detect == 'strict':
custom = '+crccheck+bitstream+buffer+explode'
else:
custom = error_detect
ffmpeg_command.insert(3, '-err_detect')
ffmpeg_command.insert(4, custom)
if threads > 0:
ffmpeg_command.append('-threads')
ffmpeg_command.append(str(threads))
try:
result = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
lines = result.stdout.splitlines()
json_output = json.dumps({i: line for i, line in enumerate(lines)}, separators=(',', ':'))
if 'error' in result.stdout.lower():
raise subprocess.CalledProcessError(result.returncode, ffmpeg_command, output=json_output, stderr=result.stderr)
except subprocess.CalledProcessError as e:
raise Exception(e.output)