Skip to content

Commit

Permalink
tools/opus: rename func and catch cover compr. error
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadSix27 committed Jan 29, 2020
1 parent 08e5b5d commit 176231a
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions tools/opus_maker/opus.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,32 +155,32 @@
import shutil

class OpusMaker:
def convertCoverToJpg(self, file_path: Path) -> Path:
temp_out_path = file_path.change_name(file_path.stem + "opusthing_xyz.jpg")
def compressCover(self, file_path: Path) -> Path:
print(F"Compressing cover {file_path}...")
temp_out_path = file_path.change_name(F"{abs(hash(file_path.stem))}_opusthing_xyz.jpg")
cmd = [
'ffmpeg',
'-y',
'-i',
str(file_path),
'-an',
'-compression_level',
'75',
'-pix_fmt',
'yuvj444p',
'-s',
'300x300',
'-c:v',
'mjpeg',
'-compression_level',
'75',
'-s',
'300x300',
str(temp_out_path),
]
try:
_ = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(e.output.decode("utf-8"))
print(F"\nConverting Cover '{file_path}' to JPG Failed, error above, command was:")
print(F"\n Compressing Cover '{file_path}' Failed, error above, command was:")
print(subprocess.list2cmdline(cmd))
exit(1)

return temp_out_path

def getFfprobe(self, file_path: Path) -> dict:
Expand Down Expand Up @@ -224,7 +224,7 @@ def hasCover(self, file_path) -> bool:

def getCoverFromFile(self, file_path: Path) -> Path:
if self.hasCover(file_path):
return self.extractCoverFromFile(file_path)
return self.compressCover(file_path)
return None

def encodeFile(self, original_file: Path) -> Path:
Expand All @@ -245,10 +245,21 @@ def encodeFile(self, original_file: Path) -> Path:
cover = self.getCoverFromFile(original_file)
if not cover:
cover = self.getCoverFromFolder(original_file)
print(F"Using cover from folder: {cover}")
cover = self.convertCoverToJpg(cover)
if cover:
cover = self.compressCover(cover)
if not cover.exists():
print("Cover compression failed, maybe missing WebP or JPEG support in ffmpeg? Or corrupt cover image file.")
exit(1)
else:
print(F"Using cover file '{cover}' in folder: {cover.parent}")
else:
print(F"Using cover from file: {original_file}")
if not cover.exists():
print("Cover compression failed, maybe missing WebP or JPEG support in ffmpeg? Or corrupt audio file.")
exit(1)
else:
print(F"Using cover '{cover}' from audio metadata in: {original_file}")
else:
print("No cover found.")

cmd1 = [
'ffmpeg',
Expand Down Expand Up @@ -294,8 +305,8 @@ def encodeFile(self, original_file: Path) -> Path:
erange = F" from start to {end_time}"

print(F"Encoding{erange} into file {output_file_path}...")
p = subprocess.Popen(cmd1, shell=False, bufsize=0, stderr=subprocess.DEVNULL, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stdin=p.stdout)
p = subprocess.Popen(cmd1, shell=False, bufsize=0, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stderr=subprocess.PIPE, stdout=subprocess.DEVNULL, stdin=p.stdout)
stdo, stde = p2.communicate()

if cover:
Expand Down Expand Up @@ -332,7 +343,6 @@ def __init__(self, input_path: Path,
overwrite_existing: bool = None,
append_time: bool = None,
) -> None:
self.extractCoverFromFile = self.convertCoverToJpg
errors = []
# Settings
self.opusVbr = opus_vbr
Expand Down

0 comments on commit 176231a

Please sign in to comment.