Av1an/Chunks/chunk.py

119 lines
3.6 KiB
Python
Raw Normal View History

2020-08-05 03:18:15 +00:00
from pathlib import Path
from typing import Dict, Any, Optional
2020-08-05 03:18:15 +00:00
2020-09-14 17:45:21 +00:00
from Av1an.commandtypes import Command
2020-08-05 03:18:15 +00:00
class Chunk:
"""
Chunk class. Stores information relating to a chunk. The command that gets the chunk and the encoding commands
to be run on this chunk.
"""
2020-08-14 00:56:54 +00:00
def __init__(self,
temp: Path,
index: int,
ffmpeg_gen_cmd: Command,
2020-08-14 00:56:54 +00:00
output_ext: str, size: int,
frames: int):
2020-08-06 05:46:49 +00:00
"""
Chunk class constructor
Note: while ffmpeg_gen_cmd is a Command, it must be serializable, so Path can't be used
2020-08-06 05:46:49 +00:00
:param temp: The temp directory
:param index: the index of this chunk
:param ffmpeg_gen_cmd: the ffmpeg command that will generate this chunk
:param output_ext: the output extension after encoding
:param size: the size of this chunk. used for sorting
:param frames: the number of frames in this chunk
"""
self.index: int = index
self.ffmpeg_gen_cmd: Command = ffmpeg_gen_cmd
self.size: int = size
self.temp: Path = temp
self.frames: int = frames
self.output_ext: str = output_ext
self.vmaf_target_cq: Optional[int] = None
2020-08-06 05:46:49 +00:00
def to_dict(self) -> Dict[str, Any]:
"""
Converts this chunk to a dictionary for easy json serialization
:return: A dictionary
"""
return {
'index': self.index,
'ffmpeg_gen_cmd': self.ffmpeg_gen_cmd,
'size': self.size,
'frames': self.frames,
'output_ext': self.output_ext,
'vmaf_target_cq': self.vmaf_target_cq,
}
2020-08-06 02:51:30 +00:00
@property
def fake_input_path(self) -> Path:
"""
Returns the mkv chunk file that would have been for this chunk in the old chunk system.
2020-08-06 05:46:49 +00:00
Ex: .temp/split/00000.mkv
2020-08-06 02:51:30 +00:00
:return: a path
"""
return (self.temp / 'split') / f'{self.name}.mkv'
2020-08-05 03:18:15 +00:00
@property
def output_path(self) -> Path:
2020-08-06 05:46:49 +00:00
"""
Gets the path of this chunk after being encoded with an extension
Ex: Path('.temp/encode/00000.ivf')
:return: the Path of this encoded chunk
"""
2020-08-05 03:18:15 +00:00
return (self.temp / 'encode') / f'{self.name}.{self.output_ext}'
2020-08-05 03:18:15 +00:00
@property
def output(self) -> str:
2020-08-06 05:46:49 +00:00
"""
Gets the posix string of this chunk's output_path (with extension)
See: Chunk.output_path
Ex: '.temp/encode/00000.ivf'
:return: the string of this chunk's output path
"""
2020-08-05 03:18:15 +00:00
return self.output_path.as_posix()
2020-08-05 03:18:15 +00:00
@property
def fpf(self) -> str:
2020-08-06 05:46:49 +00:00
"""
Gets the posix string of this chunks first pass file without an extension
Ex: '.temp/split/00000_fpf'
:return: the string of this chunk's first pass file (no extension)
"""
2020-08-05 03:18:15 +00:00
fpf_file = (self.temp / 'split') / f'{self.name}_fpf'
return fpf_file.as_posix()
2020-08-05 03:18:15 +00:00
@property
2020-08-05 19:15:45 +00:00
def name(self) -> str:
2020-08-06 05:46:49 +00:00
"""
Gets the name of this chunk. It is the index zero padded to length 5 as a string.
Ex: '00000'
:return: the name of this chunk as a string
"""
2020-08-05 03:18:15 +00:00
return str(self.index).zfill(5)
@staticmethod
def create_from_dict(d: dict, temp):
2020-08-06 05:46:49 +00:00
"""
Creates a chunk from a dictionary.
See: Chunk.to_dict
:param d: the dictionary
:param temp: the temp directory
:return: A Chunk from the dictionary
"""
chunk = Chunk(temp, d['index'], d['ffmpeg_gen_cmd'], d['output_ext'], d['size'], d['frames'])
chunk.vmaf_target_cq = d['vmaf_target_cq']
return chunk