trans coder
This commit is contained in:
parent
b1c0b8ef78
commit
6149d05684
|
@ -1,7 +1,7 @@
|
|||
from ..types import File
|
||||
from ..utils.scan_for_music import scan_for_music
|
||||
from ..utils.load_tag_information import load_tag_information
|
||||
from ..transcode_levels import transcode_levels
|
||||
from ..transcode_levels import print_transcode_levels, transcode_levels_list
|
||||
from ..utils.transcoder import transcode, get_transcode_config
|
||||
|
||||
from os import makedirs as make_directories
|
||||
|
@ -30,6 +30,10 @@ class CopyCommand():
|
|||
self.state = CopyCommandState()
|
||||
|
||||
def run(self):
|
||||
if self.transcode_level == "list":
|
||||
print_transcode_levels()
|
||||
exit()
|
||||
|
||||
print("Copying")
|
||||
self.scan_for_music()
|
||||
self.load_tag_information()
|
||||
|
@ -128,7 +132,7 @@ class CopyCommand():
|
|||
for file in self.state.files:
|
||||
self._transcode_copy(file)
|
||||
return
|
||||
elif self.transcode_level in transcode_levels:
|
||||
elif self.transcode_level in transcode_levels_list:
|
||||
for file in self.state.files:
|
||||
self._transcode_with_level(
|
||||
file, self.transcode_level)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from ..utils.transcoder import get_transcode_config, transcode, TranscodeConfig
|
||||
from ..utils.scan_for_music import file_from_path
|
||||
from ..transcode_levels import transcode_levels
|
||||
from ..transcode_levels import print_transcode_levels, transcode_levels
|
||||
|
||||
from pathlib import Path
|
||||
from json import load as load_json_file
|
||||
|
@ -22,7 +22,7 @@ class TranscodeCommand:
|
|||
|
||||
def run(self):
|
||||
if self.transcode_level == "list":
|
||||
print("Transcode Levels:", ", ".join(transcode_levels))
|
||||
print_transcode_levels()
|
||||
exit()
|
||||
|
||||
print("Transcoding...")
|
||||
|
|
|
@ -1,43 +1,63 @@
|
|||
from functools import reduce
|
||||
|
||||
def add_to_arr(arr: list[str], items: list[str]) -> list[str]:
|
||||
for item in items:
|
||||
arr.append(item)
|
||||
|
||||
# does not include copy
|
||||
transcode_levels = []
|
||||
transcode_levels = {}
|
||||
|
||||
transcode_levels["mp3"] = []
|
||||
|
||||
# mp3 v0 -> v9
|
||||
add_to_arr(transcode_levels, [
|
||||
add_to_arr(transcode_levels["mp3"], [
|
||||
f"mp3-v{quality}" for quality in range(0, 10)
|
||||
])
|
||||
# mp3 bitrates
|
||||
mp3_bitrates = [8, 16, 24, 32, 40, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
|
||||
add_to_arr(transcode_levels, [
|
||||
add_to_arr(transcode_levels["mp3"], [
|
||||
f"mp3-{bitrate}k" for bitrate in mp3_bitrates
|
||||
])
|
||||
|
||||
transcode_levels["opus"] = []
|
||||
|
||||
opus_bitrates = ["16", "24", "32", "64", "96", "128", "256"]
|
||||
add_to_arr(transcode_levels, [
|
||||
add_to_arr(transcode_levels["opus"], [
|
||||
f"opus-{bitrate}k" for bitrate in opus_bitrates
|
||||
])
|
||||
|
||||
add_to_arr(transcode_levels, [
|
||||
transcode_levels["vorbis"] = []
|
||||
|
||||
add_to_arr(transcode_levels["vorbis"], [
|
||||
f"vorbis-q{quality}" for quality in range(-2, 11)
|
||||
])
|
||||
|
||||
add_to_arr(transcode_levels, [
|
||||
transcode_levels["speex"] = []
|
||||
|
||||
add_to_arr(transcode_levels["speex"], [
|
||||
f"speex-q{quality}" for quality in range(0, 11)
|
||||
])
|
||||
|
||||
transcode_levels["g726"] = []
|
||||
|
||||
g726_bitrates = ["16", "24", "32", "40"]
|
||||
add_to_arr(transcode_levels, [
|
||||
add_to_arr(transcode_levels["g726"], [
|
||||
f"g726-{bitrate}k" for bitrate in g726_bitrates
|
||||
])
|
||||
|
||||
# Extra Default Mappings
|
||||
preset_transcode_levels = {
|
||||
preset_transcode_levels = {}
|
||||
|
||||
mp3_presets = {
|
||||
"mp3-low": "mp3-v4",
|
||||
"mp3-medium": "mp3-v2",
|
||||
"mp3-high": "mp3-v0",
|
||||
}
|
||||
|
||||
preset_transcode_levels = preset_transcode_levels | mp3_presets
|
||||
add_to_arr(transcode_levels["opus"], mp3_presets.keys())
|
||||
|
||||
opus_presets = {
|
||||
"opus-low": "opus-32k",
|
||||
"opus-medium": "opus-64k",
|
||||
"opus-high": "opus-96k",
|
||||
|
@ -45,9 +65,19 @@ preset_transcode_levels = {
|
|||
"opus-extreme": "opus-256k"
|
||||
}
|
||||
|
||||
add_to_arr(transcode_levels, preset_transcode_levels.keys())
|
||||
preset_transcode_levels = preset_transcode_levels | opus_presets
|
||||
add_to_arr(transcode_levels["opus"], opus_presets.keys())
|
||||
|
||||
#transcode_levels.sort()
|
||||
#import json
|
||||
#print(json.dumps(transcode_levels,indent=2))
|
||||
#exit()
|
||||
transcode_levels["mp3"].sort()
|
||||
transcode_levels["opus"].sort()
|
||||
transcode_levels["vorbis"].sort()
|
||||
transcode_levels["speex"].sort()
|
||||
transcode_levels["g726"].sort()
|
||||
|
||||
def print_transcode_levels():
|
||||
for category in transcode_levels.keys():
|
||||
print(f"Category {category}:")
|
||||
for level in transcode_levels[category]:
|
||||
print(f"- {level}")
|
||||
|
||||
transcode_levels_list = reduce(lambda a, b: a+b, transcode_levels.values())
|
|
@ -42,29 +42,33 @@ def get_transcode_config(file: File, level: str):
|
|||
level = preset_transcode_levels["level"]
|
||||
|
||||
if level.startswith("g726-") and level.endswith("k"):
|
||||
conf.container = "matroska"
|
||||
conf.file_extension = "mkv"
|
||||
conf.encoder = "g726"
|
||||
conf.sample_rate = "8000"
|
||||
conf.channels = "1"
|
||||
conf.use_bitrate = True
|
||||
# includes the k at end
|
||||
conf.bitrate = level.replace("g726-", "")
|
||||
conf.load_from_dict({
|
||||
"container": "matroska",
|
||||
"file_extension": "mka",
|
||||
"encoder": "g726",
|
||||
"sample_rate": "8000",
|
||||
"channels": "1",
|
||||
"use_bitrate": True,
|
||||
"bitrate": level.replace("g726-", "")
|
||||
})
|
||||
return conf
|
||||
|
||||
if level.startswith("opus-") and level.endswith("k"):
|
||||
conf.container = "ogg"
|
||||
conf.file_extension = "opus"
|
||||
conf.encoder = "libopus"
|
||||
conf.use_bitrate = True
|
||||
# includes the k at end
|
||||
conf.bitrate = level.replace("opus-", "")
|
||||
conf.load_from_dict({
|
||||
"container": "ogg",
|
||||
"file_extension": "opus",
|
||||
"encoder": "libopus",
|
||||
"use_bitrate": True,
|
||||
"bitrate": level.replace("opus-", "")
|
||||
})
|
||||
return conf
|
||||
|
||||
if level.startswith("mp3-"):
|
||||
conf.container = "mp3"
|
||||
conf.file_extension = "mp3"
|
||||
conf.encoder = "libmp3lame"
|
||||
conf.load_from_dict({
|
||||
"container": "mp3",
|
||||
"file_extension": "mp3",
|
||||
"encoder": "libmp3lame",
|
||||
})
|
||||
|
||||
if level.startswith("mp3-v"):
|
||||
conf.use_quality = True
|
||||
|
@ -76,28 +80,29 @@ def get_transcode_config(file: File, level: str):
|
|||
return conf
|
||||
|
||||
if level.startswith("vorbis-q"):
|
||||
conf.container = "ogg"
|
||||
conf.file_extension = "ogg"
|
||||
conf.encoder = "libvorbis"
|
||||
conf.use_quality = True
|
||||
conf.quality = level.replace("vorbis-q", "")
|
||||
conf.load_from_dict({
|
||||
"container": "ogg",
|
||||
"file_extension": "ogg",
|
||||
"encoder": "libvorbis",
|
||||
"use_quality": True,
|
||||
"quality": level.replace("vorbis-q", ""),
|
||||
})
|
||||
return conf
|
||||
|
||||
if level.startswith("speex-q"):
|
||||
conf.container = "ogg"
|
||||
conf.file_extension = "ogg"
|
||||
conf.encoder = "libspeex"
|
||||
conf.use_quality = True
|
||||
conf.quality = level.replace("speex-q", "")
|
||||
conf.load_from_dict({
|
||||
"container": "ogg",
|
||||
"file_extension": "ogg",
|
||||
"encoder": "libspeex",
|
||||
"use_quality": True,
|
||||
"quality": level.replace("speex-q", ""),
|
||||
})
|
||||
return conf
|
||||
|
||||
print("Unknown Level")
|
||||
exit()
|
||||
|
||||
def transcode(file: File, config: TranscodeConfig, dest: str):
|
||||
title = file.tags.title
|
||||
artist = file.tags.artist
|
||||
|
||||
ffmpeg_command = [
|
||||
ffmpeg_path,
|
||||
"-y",
|
||||
|
@ -131,12 +136,6 @@ def transcode(file: File, config: TranscodeConfig, dest: str):
|
|||
ffmpeg_command.append(config.bitrate)
|
||||
else:
|
||||
pass
|
||||
|
||||
# Add Metadata
|
||||
ffmpeg_command.append("-metadata")
|
||||
ffmpeg_command.append(f"title=\"{title}\"")
|
||||
ffmpeg_command.append("-metadata")
|
||||
ffmpeg_command.append(f"artist=\"{artist}\"")
|
||||
|
||||
ffmpeg_command.append(dest)
|
||||
print(ffmpeg_command)
|
||||
|
|
Loading…
Reference in a new issue