Files
python/handle_imported_video.py
2024-09-24 13:29:42 +02:00

124 lines
3.6 KiB
Python

import os
from posixpath import ismount
import sys
import time
import subprocess
import datetime
import shutil
import json
import getopt
try:
opts, args = getopt.getopt(sys.argv[1:], "fp:d:s:", ["command=", "help", "output=", "backup"])
except getopt.GetoptError as err:
# print help information and exit:
print(str(err)) # will print something like "option -a not recognized"
#usage()
sys.exit(2)
output = None
# QJ : getopts
_DEV = "gopro9_1"
_SEQUENCES = []
_FLIP = False
for o, a in opts:
if o == "-h":
_ACTION = True
elif o in ("-d", "--device"):
_DEV = True
elif o in ("-p", "--part"):
_DAY_DIR = a
elif o in ("-f", "--flip"):
_FLIP = True
elif o in ("-s", "--sequence"):
a = a.split(",")
for s in a:
print(s)
_SEQUENCES.append("{:04d}".format(int(s)))
else:
_WIZZARD = True
#_SOURCE_DIR = "/media/nas/nas-photo/imported/"
_SOURCE_DIR = "/share/Photo/imported/"
_FULL_PATH = _SOURCE_DIR + "/" + _DEV + "/" + _DAY_DIR
video_seq = {}
if os.path.exists(_FULL_PATH):
if os.path.exists(_FULL_PATH + "/100GOPRO"):
_FULL_PATH = _FULL_PATH + "/100GOPRO"
for filename in os.listdir(_FULL_PATH):
if filename.endswith(".MP4"):
prefix = filename[0:2]
part = prefix + filename[2:4]
seq = filename[4:8]
if not seq in video_seq:
video_seq[seq] = []
video_seq[seq].append(part + seq + ".MP4")
print(filename)
print(seq)
print(part)
print(json.dumps(video_seq,indent=2))
_PROCESSED = _FULL_PATH + "/processed"
if not os.path.exists(_PROCESSED):
os.mkdir(_PROCESSED)
print(f"SEQUENCES : {_SEQUENCES}")
for vid in video_seq:
if len(_SEQUENCES) != 0 and vid not in _SEQUENCES:
continue
export_name = "joined_" + vid + ".MP4"
parts = video_seq[vid]
parts.sort()
print(parts)
if len(parts) > 1:
print("This need to be joined!")
# for p in parts:
# join_string =
# else:
# continue
print(parts)
# print(type(parts))
#ffmpeg -i concat:"input1.mp4|input2.mp4" output.mp4
os.chdir(_FULL_PATH)
f = open("video.txt", "w")
f.write("# Files to join\n")
f.close()
f = open("video.txt", "a")
for p in parts:
f.write(f"file '{p}'\n")
f.close()
print(_FULL_PATH)
if _FLIP:
myCmd = '/opt/ffmpeg/ffmpeg -display_rotation 180 -f concat -i video.txt -vcodec copy -acodec copy ' + export_name
else:
myCmd = '/opt/ffmpeg/ffmpeg -f concat -i video.txt -vcodec copy -acodec copy ' + export_name
print(myCmd)
print(os.getcwd())
status, output = subprocess.getstatusoutput(myCmd)
#print(output)
print(status)
if status == 0:
print("Moving...")
for p in parts:
try:
shutil.move(p, _PROCESSED)
except:
print(f"Failed to move {p}")
try:
shutil.move(p.replace("MP4", "THM"), _PROCESSED)
except:
print(f"Failed to move {p.replace("MP4", "THM")}")
try:
shutil.move(p.replace("MP4", "LRV").replace("GH","GL"), _PROCESSED)
except:
print(f"Failed to move {p.replace("MP4", "LRV")}")