Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supprt mp3 chapters as alternative to tracks.txt #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions album_splitter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from yt_dlp import YoutubeDL

from .parse_tracks import parse_tracks
from .parse_tracks import parse_tracks, get_tracks_from_tags
from .split_file import split_file
from .tag_file import tag_file
from .utils.secure_filename import secure_filename
Expand Down Expand Up @@ -126,12 +126,17 @@ def main():
args.folder = "./splits/{}".format(secure_filename(args.audio_file))

print("Reading tracks file")
tracks_file = Path(args.tracks)
if not tracks_file.exists():
print(f"Can't find tracks file: {tracks_file}")
exit(-1)
tracks_content = tracks_file.read_text(encoding="utf-8", errors="ignore")
tracks = parse_tracks(tracks_content, duration=args.duration)
tracks_file = args.tracks
if not tracks_file or not Path(tracks_file).exists():
try:
tracks = get_tracks_from_tags(args.audio_file)
except Exception as e:
print(f"Can't find tracks file: {tracks_file}, and could not extract tracks from tags")
exit(-1)
else:
tracks_file = Path(tracks_file)
tracks_content = tracks_file.read_text(encoding="utf-8", errors="ignore")
tracks = parse_tracks(tracks_content, duration=args.duration)
if not len(tracks):
print("No tracks could be read/parsed from the tracks file.")
exit(-1)
Expand Down
14 changes: 14 additions & 0 deletions album_splitter/parse_tracks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import math
import re
from collections import namedtuple
import eyed3

import typing

Expand Down Expand Up @@ -56,3 +58,15 @@ def parse_line(line: str) -> typing.Tuple[str, str]:
)
title = title.strip(" -|")
return timestamp, title

def get_tracks_from_tags(audio_file):
audio = eyed3.load(audio_file)
chapters = audio.tag.chapters
tracks = []
for chapter in chapters:
title = chapter.title
times_ms = chapter.times
start_ms = times_ms[0]
start_seconds =int(math.floor(start_ms / 1000))
tracks.append(Track(title=title, start_timestamp=start_seconds))
return tracks
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies = [
'ffmpy',
'music_tag',
"yt-dlp",
"eyed3"
]
dynamic = ["version"]

Expand Down