-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_script.py
More file actions
86 lines (70 loc) · 2.53 KB
/
Copy pathcamera_script.py
File metadata and controls
86 lines (70 loc) · 2.53 KB
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
from moviepy import VideoFileClip
from datetime import datetime
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
OUTPUT_PATH = f"{Path.home()}/Pictures/Camera"
BRAND_NAME = "Olympus"
def convert_avi_to_mp4(input_file, output_file):
"""
Camera automatically records in .avi, which is not always readable
"""
clip = VideoFileClip(input_file)
clip.write_videofile(output_file, codec="libx264")
clip.close()
def detect_camera():
"""
Detect camera using gphoto2
"""
subprocess.run(
["killall", "gvfs-gphoto2-volume-monitor"], stderr=subprocess.DEVNULL
)
subprocess.run(["killall", "gvfsd-mtp"], stderr=subprocess.DEVNULL)
subprocess.run(["killall", "gvfsd-gphoto2"], stderr=subprocess.DEVNULL)
camera = subprocess.run(
["gphoto2", "--auto-detect"], encoding="utf-8", stdout=subprocess.PIPE
).stdout
if BRAND_NAME not in camera:
raise Exception(f"{BRAND_NAME} camera NOT detected. Camera might be empty.")
def delete_files():
"""
Remove files on camera
"""
response = input(
"Do you with to remove all photos from the camera? IMPORTANT! This cannot be undone. [y/n] "
)
if response != "y":
return
double_check = input("Are you sure that you would like to delete? [y/n] ")
if double_check != "y":
return
subprocess.run(["gphoto2", "--delete-all-files"])
print("Files deleted.")
def main():
detect_camera()
with tempfile.TemporaryDirectory() as temp_dir:
print(f"Copying all photos to {temp_dir}")
subprocess.run(["gphoto2", "--get-all-files"], cwd=temp_dir, check=True)
for filename in os.listdir(temp_dir):
photo_location = os.path.join(temp_dir, filename)
m_time = os.path.getmtime(photo_location)
date = datetime.fromtimestamp(m_time).strftime("%Y.%m.%d")
if filename.lower().endswith(".avi"):
video_path = os.path.join(
temp_dir, os.path.splitext(filename)[0] + ".mp4"
)
convert_avi_to_mp4(photo_location, video_path)
os.remove(photo_location)
photo_location = video_path
final_location = f"{OUTPUT_PATH}/{date}"
if not os.path.isdir(final_location):
os.makedirs(final_location)
try:
shutil.move(photo_location, final_location)
except shutil.Error:
continue
delete_files()
if __name__ == "__main__":
main()