diff --git a/.gitignore b/.gitignore index 9dafc72..99161d5 100644 --- a/.gitignore +++ b/.gitignore @@ -161,3 +161,4 @@ cython_debug/ #.idea/ *local +out/* diff --git a/out/.gitkeep b/out/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/split.py b/split.py index eb19d47..771dc10 100644 --- a/split.py +++ b/split.py @@ -4,77 +4,54 @@ # split.py # # Copyright 2023 Beat Jäckle -from ffmpeg import FFmpeg +""" +Module providingClasses reading Files +Module providingFunction writing mediafiles for us +""" from splitSrc.ChapterTitle import ChapterTitle from splitSrc.ChapterTimes import ChapterTimes +from splitSrc.ffmpeg import ffmpeg + FILENAMEMUSTER = ['../HPMoR_Part_', '.mp3'] TITLES = 'chaptertitle.ods' TIMES = 'chapter.csv' +FILEFORMAT = 'mp3' -def ffmpeg(part, cid, time: dict, end, title) -> None: - # utitle = title.replace(" ", "_") - # utitle = utitle.replace("'", "_") - # utitle = utitle.replace("__", "_") - utitle = title['t_'] - title = title['t'] - filename = f'{cid}_{utitle}' - f = FFmpeg() - f.option('y') - if end is not None: - f.input( - FILENAMEMUSTER[0] + - str(part) + - FILENAMEMUSTER[1], - ss=time, - to=end, - - ) - else: - f.input( - FILENAMEMUSTER[0] + - str(part) + - FILENAMEMUSTER[1], - ss=time, - to=end, - - ) - f.output( - filename, - metadata=f'title={title}' - ) - f.execute() - - -def main(args): - chapterTitle = ChapterTitle(TITLES) - chapterTimes = ChapterTimes() - current = next(chapterTimes) - for pochain in chapterTimes: - [i, p, t] = current +def main(): + """Splitting HPmod Files into their chapter""" + chapter_title = ChapterTitle(TITLES) + chapter_times = ChapterTimes() + current = next(chapter_times) + for pochain in chapter_times: + [cid, part, time] = current try: - i = int(i) + cid = int(cid) except ValueError: - print(i, t, 'Keine Kapitelnummer') + print(cid, time, 'Keine Kapitelnummer') else: # Test if it has a processor) - if p == pochain[1]: + if part == pochain[1]: end = pochain[2] else: end = None try: - title = chapterTitle[i] + title = chapter_title[cid] except KeyError: - print('No Chapter',i) - title = dict(t='__No Name__', t_='UNKNOWN') - ffmpeg( - part=p, - cid=i, - time=t, - end=end, - title=title) + print('No Chapter', cid) + title = dict(time='__No Name__', time_='UNKNOWN') + else: + ffmpeg( + part=part, + cid=cid, + time=time, + end=end, + title=title, + filenamemuster=FILENAMEMUSTER, + fileformat=FILEFORMAT, + ) finally: current = pochain return 0 @@ -82,4 +59,4 @@ def main(args): if __name__ == '__main__': import sys - sys.exit(main(sys.argv)) + sys.exit(main()) diff --git a/splitSrc/ffmpeg.py b/splitSrc/ffmpeg.py new file mode 100644 index 0000000..0507c99 --- /dev/null +++ b/splitSrc/ffmpeg.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- +# +# ffmpeg.py +# +# Copyright 2023 Beat Jäckle +from ffmpeg import FFmpeg +import asyncio + + +async def main(part, cid, time: dict, end, title, filenamemuster, fileformat): + utitle = title['t_'] + title = title['t'] + filename = f'out/{cid}_{utitle}.{fileformat}' + + ffmpeg = ( + FFmpeg() + .option("y") + ) + if end is not None: + ffmpeg.input( + filenamemuster[0] + + str(part) + + filenamemuster[1], + ss=time, + to=end, + + ) + else: + ffmpeg.input( + filenamemuster[0] + + str(part) + + filenamemuster[1], + ss=time, + to=end, + ) + ffmpeg.output( + filename, + # metadata=[f'title={title}', f'track={cid}'], + options={ + 'metadata': [f'title={title}', f'track={cid}'], + "codec:a": "copy" + } + ) + + @ffmpeg.on("start") + def on_start(arguments): + print("Arguments:", arguments) + + @ffmpeg.on("stderr") + def on_stderr(line): + print("stderr:", line) + + @ffmpeg.on("progress") + def on_progress(progress): + print(progress) + + @ffmpeg.on("progress") + def time_to_terminate(progress): + # Gracefully terminate when more than 200 frames are processed + if progress.frame > 200: + ffmpeg.terminate() + + @ffmpeg.on("completed") + def on_completed(): + print("Completed") + + @ffmpeg.on("terminated") + def on_terminated(): + print("Terminated") + + @ffmpeg.on("error") + def on_error(code): + print("Error:", code) + + await ffmpeg.execute() + + +def ffmpeg(part, cid, time: dict, end, title, filenamemuster, fileformat): + asyncio.run(main( + part, cid, time, end, title, filenamemuster, fileformat + ))