-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathffmpeg_utils.py
79 lines (74 loc) · 2.64 KB
/
ffmpeg_utils.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import sys
import json
import time
import ffmpeg
from subprocess import call, check_output
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
def get_codec(filepath, channel='v:0'):
output = check_output(['ffprobe', '-v', 'error', '-select_streams', channel,
'-show_entries', 'stream=codec_name,codec_tag_string', '-of',
'default=nokey=1:noprint_wrappers=1', filepath])
return output.decode('utf-8').split()
def encode(filepath):
basefilepath, extension = os.path.splitext(filepath)
output_filepath = basefilepath + '.HEVC' + '.mp4'
assert(output_filepath != filepath)
if os.path.isfile(output_filepath):
print('Skipping "{}": file already exists'.format(output_filepath))
return None
print(filepath)
# Get the video channel codec
video_codec = get_codec(filepath, channel='v:0')
if video_codec == []:
print('Skipping: no video codec reported')
return None
# Video transcode options
if video_codec[0] == 'hevc':
if video_codec[1] == 'hvc1':
print('Skipping: already h265 / hvc1')
return None
else:
# Copy stream to hvc1
video_opts = '-c:v copy -tag:v hvc1'
else:
# Transcode to h265 / hvc1
video_opts = '-c:v libx265 -crf 28 -tag:v hvc1 -preset fast -threads 8'
# Get the audio channel codec
audio_codec = get_codec(filepath, channel='a:0')
if audio_codec == []:
audio_opts = ''
elif audio_codec[0] == 'aac':
audio_opts = '-c:a copy'
else:
audio_opts = '-c:a aac -b:a 128k'
call(['ffmpeg', '-i', filepath] + video_opts.split() + audio_opts.split() + [output_filepath])
os.remove(filepath)
return output_filepath
def get_thumbnail(in_filename, path, ttl):
out_filename = os.path.join(path, str(time.time()) + ".jpg")
open(out_filename, 'a').close()
try:
(
ffmpeg
.input(in_filename, ss=ttl)
.output(out_filename, vframes=1)
.overwrite_output()
.run(capture_stdout=True, capture_stderr=True)
)
return out_filename
except ffmpeg.Error as e:
return None
def get_duration(filepath):
metadata = extractMetadata(createParser(filepath))
if metadata.has("duration"):
return metadata.get('duration').seconds
else:
return 0
def get_width_height(filepath):
metadata = extractMetadata(createParser(filepath))
if metadata.has("width") and metadata.has("height"):
return metadata.get("width"), metadata.get("height")
else:
return 1280, 720