Skip to content

Commit

Permalink
Add ability to just add Samsung trailer with video without modifying …
Browse files Browse the repository at this point in the history
…XMP (probably only useful for debug purposes)
  • Loading branch information
PetrVys committed Nov 13, 2024
1 parent a68b65a commit 503daba
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 51 deletions.
109 changes: 58 additions & 51 deletions Muxer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
delete_video: bool = False,
delete_temp: bool = True,
overwrite: bool = False,
no_xmp: bool = False,
verbose: bool = False,
):
self.logger = logging.getLogger(Path(image_fpath).stem)
Expand All @@ -48,6 +49,7 @@ def __init__(
self.output_directory = output_directory
self.overwrite = overwrite
self.delete_video = delete_video
self.no_xmp = no_xmp

if os.path.isfile(self.image_fpath) is False:
self.logger.error("Image file doesn't exist")
Expand Down Expand Up @@ -205,61 +207,66 @@ def mux(self):
self.fix_output_fpath(image_metadata)
self.validate_video(self.video_fpath, metadata=video_metadata)

result = et.execute(
*[
"-X",
"-ee",
"-n",
"-QuickTime:StillImageTime",
"-QuickTime:TrackDuration",
f"{self.video_fpath}",
]
)

try:
track_number = extract_track_number(result)
self.logger.info("Live Photo keyframe track number: %s", track_number)
if self.no_xmp is False:
result = et.execute(
*[
"-X",
"-ee",
"-n",
"-QuickTime:StillImageTime",
"-QuickTime:TrackDuration",
f"{self.video_fpath}",
]
)

try:
track_number = extract_track_number(result)
self.logger.info("Live Photo keyframe track number: %s", track_number)

track_duration = extract_track_duration(track_number, result)
self.logger.info("Live Photo keyframe: %sus", track_duration)

self.xmp.find(".//rdf:Description", const.NAMESPACES).set(
const.GCAMER_TIMESTAMP_US,
str(track_duration),
)
except:
track_duration = -1
self.logger.info("Could not read Live Photo keyframe (source video is probably not from Live Photo). No keyframe will be set.")

track_duration = extract_track_duration(track_number, result)
self.logger.info("Live Photo keyframe: %sus", track_duration)
video_data = read_file(self.video_fpath)
samsung_tail = SamsungTags(video_data, image_type)

self.xmp.find(".//rdf:Description", const.NAMESPACES).set(
const.GCAMER_TIMESTAMP_US,
str(track_duration),
result = et.execute(*["-XMP", "-b", f"{self.image_fpath}"])
if result == "":
self.logger.warning("XMP of original file is empty")
else:
self.merge_xmp(result)

self.change_xmpresource(str(samsung_tail.get_video_size()), attribute=const.ITEM_LENGTH, semantic="MotionPhoto")
self.change_xmpresource(str(samsung_tail.get_image_padding()), attribute=const.ITEM_PADDING, semantic="Primary")

xmp_updated = self.output_fpath + ".XMP"
with open(xmp_updated, "wb") as f:
f.write(etree.tostring(self.xmp, pretty_print=True))

xmp_image = enrich_fname(self.output_fpath, "XMP")
shutil.copyfile(self.image_fpath, xmp_image)
et.execute(
*[
"-overwrite_original",
"-tagsfromfile",
xmp_updated,
"-xmp",
xmp_image,
]
)
except:
track_duration = -1
self.logger.info("Could not read Live Photo keyframe (source video is probably not from Live Photo). No keyframe will be set.")

video_data = read_file(self.video_fpath)
samsung_tail = SamsungTags(video_data, image_type)

result = et.execute(*["-XMP", "-b", f"{self.image_fpath}"])
if result == "":
self.logger.warning("XMP of original file is empty")
else:
self.merge_xmp(result)

self.change_xmpresource(str(samsung_tail.get_video_size()), attribute=const.ITEM_LENGTH, semantic="MotionPhoto")
self.change_xmpresource(str(samsung_tail.get_image_padding()), attribute=const.ITEM_PADDING, semantic="Primary")

xmp_updated = self.output_fpath + ".XMP"
with open(xmp_updated, "wb") as f:
f.write(etree.tostring(self.xmp, pretty_print=True))

xmp_image = enrich_fname(self.output_fpath, "XMP")
shutil.copyfile(self.image_fpath, xmp_image)
et.execute(
*[
"-overwrite_original",
"-tagsfromfile",
xmp_updated,
"-xmp",
xmp_image,
]
)

merged_bytes = read_file(xmp_image)
merged_bytes = read_file(xmp_image)
else:
video_data = read_file(self.video_fpath)
samsung_tail = SamsungTags(video_data, image_type)
merged_bytes = read_file(self.image_fpath)
samsung_tail.set_image_size(len(merged_bytes))
video_footer = samsung_tail.video_footer()
merged_bytes += video_footer
Expand Down
8 changes: 8 additions & 0 deletions motionphoto2.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
action="store_true",
help="Keep temp file used during muxing",
)
parser.add_argument(
"-nx",
"--no-xmp",
action="store_true",
help="No XMP processing (just glue image and video using Samsung tags)",
)
parser.add_argument(
"-o",
"--overwrite",
Expand Down Expand Up @@ -124,6 +130,7 @@
delete_video=args.delete_video,
delete_temp=not args.keep_temp,
overwrite=args.overwrite,
no_xmp=args.no_xmp,
verbose=args.verbose,
).mux()
print("=" * 25)
Expand All @@ -137,5 +144,6 @@
delete_video=args.delete_video,
delete_temp=not args.keep_temp,
overwrite=args.overwrite,
no_xmp=args.no_xmp,
verbose=args.verbose,
).mux()

0 comments on commit 503daba

Please sign in to comment.