-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.py
207 lines (161 loc) · 5.76 KB
/
tasks.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3.6
from __future__ import absolute_import
import os
from better_ffmpeg_progress import FfmpegProcess
from pymediainfo import MediaInfo
from resolve_proxy_encoder import helpers
from resolve_proxy_encoder.helpers import (
get_rich_logger,
install_rich_tracebacks,
app_exit,
)
from resolve_proxy_encoder.settings.app_settings import Settings
from resolve_proxy_encoder.worker.celery import app
from resolve_proxy_encoder.worker.helpers import check_wsl, get_wsl_path
settings = Settings()
config = settings.user_settings
logger = get_rich_logger(config["celery_settings"]["worker_loglevel"])
install_rich_tracebacks()
git_full_sha = helpers.get_package_current_commit("resolve_proxy_encoder")
if not git_full_sha:
logger.error(
"[red]Couldn't get local package commit SHA!\n"
+ "Necessary to prevent version mismatches between queuer and worker.[/]"
)
app_exit(1, -1)
git_short_sha = git_full_sha[::8]
@app.task(
acks_late=True,
track_started=True,
prefetch_limit=1,
soft_time_limit=60,
queue=git_short_sha,
)
def encode_proxy(job):
"""
Celery task to encode proxy media using parameters in job argument
and user-defined settings
"""
# Variables
try:
proxy_settings = config["proxy_settings"]
# Get required job metadata
clip_name = job["Clip Name"]
source_file = job["File Path"]
expected_proxy_dir = job["Expected Proxy Dir"]
# Get proxy settings
vid_codec = proxy_settings["vid_codec"]
vid_profile = proxy_settings["vid_profile"]
pix_fmt = proxy_settings["pix_fmt"]
h_res = proxy_settings["h_res"]
v_res = proxy_settings["v_res"]
proxy_ext = proxy_settings["ext"]
audio_codec = proxy_settings["audio_codec"]
audio_samplerate = proxy_settings["audio_samplerate"]
misc_args = proxy_settings["misc_args"]
except KeyError as e:
logger.error(f"Job missing required key: {e}")
raise e
if check_wsl():
expected_proxy_dir = get_wsl_path(expected_proxy_dir)
# Create path for proxy first
try:
os.makedirs(
expected_proxy_dir,
exist_ok=True,
)
except OSError as e:
logger.error(f"Error creating proxy directory: {e}")
raise e
output_file = os.path.join(
expected_proxy_dir,
os.path.splitext(clip_name)[0] + proxy_ext,
)
def get_orientation(job):
"""Get the video orientation from job metadata in FFmpeg syntax
Some footage is shot upside-down. If we flip the clip from Resolve's clip attributes,
we need to make sure we flip the proxy as well. Unforunately flipping proxies isn't applied
in realtime as needed, so proxies will need to be rerendered if orientation changes.
Args:
job (dict): Resolve job metadata
Returns:
flip_logic (str): FFmpeg syntax for video orientation
Raises:
KeyError: If the job metadata doesn't contain the necessary orientation keys
"""
try:
flip = str()
if job["H-FLIP"] == "On":
flip += " hflip, "
if job["V-FLIP"] == "On":
flip += "vflip, "
except KeyError as e:
logger.error("Couldn't get video orientation from job metadata!")
raise e
return flip
def get_timecode(job):
"""Get timecode using MediaInfo"""
file_path = job["File Path"]
media_info = MediaInfo.parse(file_path)
data_track_exists = False
for track in media_info.tracks:
if track.track_type == "Other":
data_track_exists = True
if track.time_code_of_first_frame:
logger.info("Using embedded timecode.")
return str(track.time_code_of_first_frame)
if data_track_exists:
logger.warning("Couldn't get timecode from media's data track.")
else:
logger.info(
"No embedded timecode found in media file. Using default timecode."
)
return "00:00:00:00"
ffmpeg_command = [
"ffmpeg",
"-y", # Never prompt!
*misc_args, # User global settings
"-i",
source_file,
"-c:v",
vid_codec,
"-profile:v",
vid_profile,
"-vsync",
"-1", # Necessary to match VFR
"-vf",
f"scale={h_res}:{v_res},{get_orientation(job)}" f"format={pix_fmt}",
"-c:a",
audio_codec,
"-ar",
audio_samplerate,
"-timecode",
get_timecode(job),
output_file,
]
logger.info(f"FFmpeg command:\n{' '.join(ffmpeg_command)}")
# TODO: Fix terminal progress output on newline
# Currently BetterFFMpegProgress is logging progress increments to newlines.
# It's better than nothing, but it looks awful. There are other issues with BFP too.
# Logging to files is messy, etc. Maybe steal the FFMpeg progress parsing func and
# use rich.progress instead.
# labels: bug
process = FfmpegProcess(
command=[*ffmpeg_command], ffmpeg_loglevel=proxy_settings["ffmpeg_loglevel"]
)
# Per-file encode log location
encode_log_dir = os.path.join(
os.path.dirname(output_file),
"encoding_logs",
)
# Make logs subfolder
os.makedirs(encode_log_dir, exist_ok=True)
encode_log_file = os.path.join(
encode_log_dir, os.path.splitext(os.path.basename(output_file))[0] + ".txt"
)
# Run encode job
try:
process.run(ffmpeg_output_file=encode_log_file)
except Exception as e:
logger.exception(f"[red] :warning: Couldn't encode proxy.[/]\n{e}")
return f"{source_file} encoded successfully"