-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmotionphoto2.py
295 lines (257 loc) · 9.82 KB
/
motionphoto2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
import argparse
import sys
import os
import exiftool
import logging
from pathlib import Path
from gooey import GooeyParser
from Muxer import Muxer
logging.basicConfig(
handlers=[logging.StreamHandler(sys.stdout)],
level=logging.DEBUG,
format="[%(asctime)s] [%(levelname)s] [%(name)s] - %(message)s",
datefmt="%d/%m/%Y %H:%M:%S"
)
def main():
parser = GooeyParser(
prog="MotionPhoto2",
description="Mux HEIC and JPG Live Photos into Google/Samsung Motion Photos",
)
dir_group = parser.add_argument_group(
"Process a Directory"
)
dir_group.add_argument(
"-id",
"--input-directory",
metavar="Input Directory",
help="Mux all the photos and videos in a directory",
widget='DirChooser',
gooey_options={'full_width':True}
)
dir_group.add_argument(
"-r",
"--recursive",
metavar="Recursive",
action="store_true",
help="Recursively process subdirectories in input_directory",
gooey_options={'initial_value':True}
)
dir_group.add_argument(
"-od",
"--output-directory",
metavar="Output Directory",
help="Directory where to save the resulting Live Photos",
widget='DirChooser',
gooey_options={'full_width':True}
)
settings_group = parser.add_argument_group(
"Settings",
gooey_options={'columns':4}
)
settings_group.add_argument(
"-dv",
"--delete-video",
metavar="Delete Video",
action="store_true",
help="Delete video after muxing",
)
settings_group.add_argument(
"-o",
"--overwrite",
metavar="Overwrite",
action="store_true",
help="Overwrite the original image",
)
settings_group.add_argument(
"-kt",
"--keep-temp",
metavar="Keep Temp",
action="store_true",
help="Keep muxing temp files",
)
settings_group.add_argument(
"-v",
"--verbose",
metavar="Verbose",
action="store_true",
help="Verbose output"
)
file_group = parser.add_argument_group(
"Process a Single File"
)
file_group.add_argument(
"-ii",
"--input-image",
metavar="Input Image",
help="Input file image (.heic, .jpg)",
widget='FileChooser',
gooey_options={
'wildcard':
"HEIC file|*.heic|"
"HEIF file|*.heif|"
"JPG file|*.jpg|"
"All files (*.*)|*.*",
'message': "Select image file"
}
)
file_group.add_argument(
"-iv",
"--input-video",
metavar="Input Video",
help="Input file video (.mov, .mp4)",
widget='FileChooser',
gooey_options={
'wildcard':
"MOV file|*.mov|"
"MP4 file|*.mp4|"
"All files (*.*)|*.*",
'message': "Select video file"
}
)
file_group.add_argument(
"-of",
"--output-file",
metavar="Output File",
help="Output Live Photo filename",
widget='FileSaver',
gooey_options={
'wildcard':
"HEIC file|*.heic|"
"HEIF file|*.heif|"
"JPG file|*.jpg|"
"All files (*.*)|*.*",
'message': "Target image file"
}
)
file_group.add_argument(
"-nx",
"--no-xmp",
metavar="No XMP",
action="store_true",
help="No XMP processing (just glue image and video using Samsung tags)",
gooey_options={'visible':False}
)
args = parser.parse_args()
if args.input_directory is not None and (
args.input_image is not None or args.input_video is not None
):
print("[ERROR] Input directory cannot be use with input-image or input-video")
sys.exit(1)
if args.input_directory is None:
if args.input_image is None or args.input_video is None:
print("[ERROR] Please provide both input image/video or input directory")
sys.exit(1)
if args.output_directory is not None and args.overwrite is True:
print("[ERROR] Output directory cannot be use overwrite option")
sys.exit(1)
if args.output_file is not None and args.overwrite is True:
print("[ERROR] Output file cannot be use overwrite option")
sys.exit(1)
if args.output_directory is not None:
output_directory = f"{Path(args.output_directory).resolve()}"
if os.path.exists(output_directory) is False:
os.mkdir(output_directory)
logger = logging.getLogger("ExifTool")
logger.setLevel(logging.DEBUG if args.verbose else logging.INFO)
with exiftool.ExifToolHelper(
encoding="utf-8",
logger=logger if args.verbose is True else None
) as et:
if args.input_directory is not None:
print(f"Converting files in {args.input_directory}")
input_directory = Path(args.input_directory).resolve()
# Get files based on recursive flag
files = []
if args.recursive:
# Recursive mode: walk through all subdirectories
for root, _, filenames in os.walk(input_directory):
for filename in filenames:
full_path = Path(root) / filename
rel_path = full_path.relative_to(input_directory)
files.append(str(rel_path))
else:
# Non-recursive mode: only process files in the specified directory
for filename in os.listdir(input_directory):
if os.path.isfile(input_directory / filename):
files.append(filename)
# Filter videos and images while preserving relative paths
videos = [
f for f in files
if Path(f).suffix.lower() in [".mp4", ".mov"]
]
images = [
f for f in files
if Path(f).suffix.lower() in [".heic", ".heif", ".avif", ".jpg", ".jpeg"]
]
i = 0
for image in images:
i += 1
image_path = Path(image)
fname = str(image_path.with_suffix(''))
# Check for both regular video filename and one with _HEVC suffix
possible_video_names = [
fname, # Original name
f"{fname}_HEVC" # Name with _HEVC suffix
]
for video_name in possible_video_names:
for ext in [".mp4", ".mov", ".MP4", ".MOV"]:
video_fname = video_name + ext
if video_fname in videos:
print(f"=========================[{i}/{len(images)}]")
video = videos.pop(videos.index(video_fname))
# Construct full paths for input files
input_image = input_directory / image
input_video = input_directory / video
# Handle output directory structure
output_subdirectory = args.output_directory
if output_subdirectory is not None:
# Preserve directory structure in output
output_subdirectory = Path(output_subdirectory) / image_path.parent
output_subdirectory = output_subdirectory.resolve()
if not output_subdirectory.exists():
output_subdirectory.mkdir(parents=True, exist_ok=True)
Muxer(
image_fpath=str(input_image),
video_fpath=str(input_video),
exiftool=et,
output_directory=str(output_subdirectory) if output_subdirectory else None,
delete_video=args.delete_video,
delete_temp=not args.keep_temp,
overwrite=args.overwrite,
no_xmp=args.no_xmp,
verbose=args.verbose,
).mux()
break
else:
continue
break
print("=" * 25)
else:
Muxer(
image_fpath=args.input_image,
video_fpath=args.input_video,
exiftool=et,
output_fpath=args.output_file,
output_directory=args.output_directory,
delete_video=args.delete_video,
delete_temp=not args.keep_temp,
overwrite=args.overwrite,
no_xmp=args.no_xmp,
verbose=args.verbose,
).mux()
if __name__ == "__main__":
if len(sys.argv) == 1:
from gooey import Gooey
main = Gooey(program_name='MotionPhoto2',
default_size=(1100, 820),
progress_regex=r"^=+\[(\d+)/(\d+)]$",
progress_expr="x[0] / x[1] * 100",
show_restart_button=False,
)(main)
# Gooey reruns the script with this parameter for the actual execution.
# Since we don't use decorator to enable commandline use, remove this parameter
# and just run the main when in commandline mode.
if '--ignore-gooey' in sys.argv:
sys.argv.remove('--ignore-gooey')
main()