Skip to content

Commit

Permalink
Small code cleanup, and add ability to specify output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaphoenix committed Jul 9, 2021
1 parent 298621d commit 3a70a02
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pymplschapters

Extract chapters from a blu-ray mpls to a matroska recognized xml file

## Installation
Expand All @@ -9,7 +10,7 @@ Extract chapters from a blu-ray mpls to a matroska recognized xml file

pymplschapters -p "C:/A Path/To/The/Playlist.mpls"

It will place any found chapters next to the input playlist file.
It will place any found chapters next to the input playlist file or to a specified directory with `-d`.

## Credit

Expand Down
5 changes: 5 additions & 0 deletions package-and-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rm -r build
rm -r dist
rm -r "pyd2v.egg-info"
python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*
5 changes: 5 additions & 0 deletions package-and-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rm -r build
rm -r dist
rm -r "pyd2v.egg-info"
python3 setup.py sdist bdist_wheel
sudo pip install .
47 changes: 34 additions & 13 deletions pymplschapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,48 @@
# --------------------------------
def main():
ArgParser = argparse.ArgumentParser()
ArgParser.add_argument('-p', '--playlist', help='Input path to an MPLS playlist', required=True)
ArgParser.add_argument(
"-p", "--playlist", help="Input path to an MPLS playlist", required=True
)
ArgParser.add_argument(
"-d",
"--directory",
help="Specify an output directory, by default it will save next to the playlist.",
required=False
)
args = ArgParser.parse_args()

def get_chapters(mpls):
header, _ = MPLS.load_header(mpls)

mpls.seek(header['PlayListStartAddress'], os.SEEK_SET)
mpls.seek(header["PlayListStartAddress"], os.SEEK_SET)
pl, _ = MPLS.load_PlayList(mpls)
pl = pl['PlayItems']
pl = pl["PlayItems"]

mpls.seek(header['PlayListMarkStartAddress'], os.SEEK_SET)
mpls.seek(header["PlayListMarkStartAddress"], os.SEEK_SET)
marks, _ = MPLS.load_PlayListMark(mpls)
marks = marks["PlayListMarks"]

for i, playItem in enumerate(pl):
chapters = []
playItemMarks = [x for x in marks if x["MarkType"] == 1 and x["RefToPlayItemID"] == i]
playItemMarks = [
x for x in marks if x["MarkType"] == 1 and x["RefToPlayItemID"] == i
]
offset = playItemMarks[0]["MarkTimeStamp"]
if playItem["INTime"] < offset:
offset = playItem["INTime"]
for n, mark in enumerate(playItemMarks):
duration = ((mark["MarkTimeStamp"] - offset) / 45000) * 1000
timespan = str(datetime.timedelta(milliseconds=duration))
if timespan == "0:00:00":
timespan = timespan + ".000000"
timespan = f"{timespan}.000000"
if timespan.startswith("0:"):
timespan = "0" + timespan
timespan = f"0{timespan}"
chapters.append({
"clip": playItem["ClipInformationFileName"] + "." + playItem["ClipCodecIdentifier"].lower(),
"number": n+1,
"clip": f"{playItem['ClipInformationFileName']}.{playItem['ClipCodecIdentifier'].lower()}",
"number": n + 1,
"duration": duration,
"timespan": timespan
"timespan": timespan,
})
yield chapters

Expand All @@ -61,7 +71,7 @@ def get_chapters(mpls):
ChapterAtom = etree.SubElement(EditionEntry, "ChapterAtom")
ChapterDisplay = etree.SubElement(ChapterAtom, "ChapterDisplay")
ChapterString = etree.SubElement(ChapterDisplay, "ChapterString")
ChapterString.text = "Chapter " + str(chapter["number"]).zfill(2)
ChapterString.text = f"Chapter {str(chapter['number']).zfill(2)}"
ChapterLanguage = etree.SubElement(ChapterDisplay, "ChapterLanguage")
ChapterLanguage.text = "eng"
ChapterTimeStart = etree.SubElement(ChapterAtom, "ChapterTimeStart")
Expand All @@ -71,7 +81,18 @@ def get_chapters(mpls):
ChapterFlagEnabled = etree.SubElement(ChapterAtom, "ChapterFlagEnabled")
ChapterFlagEnabled.text = "1"
clip = file_with_chapter[0]["clip"]
with open(os.path.join(os.path.dirname(args.playlist), os.path.splitext(os.path.basename(args.playlist))[0] + "_" + clip.split('.')[0] + ".xml"), 'wb') as f:
f.write(etree.tostring(Chapters, encoding='utf-8', doctype="<!DOCTYPE Tags SYSTEM \"matroskatags.dtd\">", xml_declaration=True, pretty_print=True))
with open(os.path.join(
args.directory or os.path.dirname(args.playlist),
f"{os.path.splitext(os.path.basename(args.playlist))[0]}_{clip.split('.')[0]}.xml"
), "wb") as f:
f.write(
etree.tostring(
Chapters,
encoding="utf-8",
doctype='<!DOCTYPE Tags SYSTEM "matroskatags.dtd">',
xml_declaration=True,
pretty_print=True,
)
)
print("Extracted chapters for " + clip)
print("Finished...")
20 changes: 8 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

setup(
name="pymplschapters",
version="1.0.2",
author="PRAGMA",
author_email="[email protected]",
version="1.0.4",
author="PHOENiX",
author_email="[email protected]",
description="Extract chapters from a blu-ray mpls to a matroska recognized xml file",
license='MIT',
license="MIT",
long_description=readme,
long_description_content_type="text/markdown",
url="https://github.com/imPRAGMA/pymplschapters",
url="https://github.com/rlaPHOENiX/pymplschapters",
packages=find_packages(),
install_requires=[
'lxml>=4.4.1'
],
entry_points={
'console_scripts': ['pymplschapters=pymplschapters.__init__:main'],
},
install_requires=["lxml>=4.4.1"],
entry_points={"console_scripts": ["pymplschapters=pymplschapters.__init__:main"]},
classifiers=[
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
],
)

0 comments on commit 3a70a02

Please sign in to comment.