mirror of
https://gitlab.sectorq.eu/jaydee/imported_video.git
synced 2025-12-14 02:24:53 +01:00
439 lines
17 KiB
Python
439 lines
17 KiB
Python
import os
|
|
from posixpath import ismount
|
|
import sys
|
|
import time
|
|
import subprocess
|
|
import datetime
|
|
import shutil
|
|
import json
|
|
import getopt
|
|
from ffmpeg_progress_yield import FfmpegProgress
|
|
from tqdm import tqdm
|
|
import filedate
|
|
def d_print(msg):
|
|
if _TEST:
|
|
print(msg)
|
|
|
|
if sys.platform == "linux" or sys.platform == "linux2":
|
|
# linux
|
|
ops = "linux"
|
|
elif sys.platform == "darwin":
|
|
# OS X
|
|
ops = "osx"
|
|
elif sys.platform == "win32":
|
|
ops = "win"
|
|
try:
|
|
opts, args = getopt.getopt(sys.argv[1:], "q:x:ta:r:fp:d:s:z:w:", ["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
|
|
_TEST = False
|
|
_FILTER = False
|
|
_QUALITY = ["35","35","25"]
|
|
if ops == "win":
|
|
_SOURCE_DIR = "p:\\imported\\"
|
|
else:
|
|
_SOURCE_DIR = "/share/Photo/imported/"
|
|
for o, a in opts:
|
|
if o == "-h":
|
|
_ACTION = True
|
|
elif o in ("-d", "--device"):
|
|
_DEV = True
|
|
elif o in ("-q", "--quality"):
|
|
_QUALITY = a.split(",")
|
|
elif o in ("-p", "--part"):
|
|
_DAY_DIRS = a.split(",")
|
|
elif o in ("-f", "--flip"):
|
|
_FLIP = True
|
|
elif o in ("-w", "--flip"):
|
|
_FILTER = True
|
|
_PATERN = a
|
|
elif o in ("-r", "--source"):
|
|
_SOURCE_DIR = a
|
|
elif o in ("-a", "--action"):
|
|
_ACTION = a
|
|
elif o in ("-t", "--test"):
|
|
_TEST = True
|
|
elif o in ("-x", "--file"):
|
|
_FILE = a
|
|
elif o in ("-z", "--file"):
|
|
_SRC_FILE = a
|
|
|
|
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/"
|
|
|
|
|
|
|
|
|
|
|
|
#print(_FULL_PATH)
|
|
|
|
def modify_creation_date(path,src_file):
|
|
print(path)
|
|
print(src_file)
|
|
create_date = filedate.File(src_file).get()
|
|
print(create_date)
|
|
filedate.File(path).set(
|
|
created = create_date["created"],
|
|
modified = create_date["modified"],
|
|
accessed = create_date["accessed"]
|
|
)
|
|
|
|
def export_gps(_DEV,_SOURCE_DIR,_DAY_DIRS):
|
|
for _DAY_DIR in _DAY_DIRS:
|
|
if ops == "win":
|
|
_FULL_PATH = _SOURCE_DIR + "\\" + _DEV + "\\" + _DAY_DIR + "\\"
|
|
else:
|
|
_FULL_PATH = _SOURCE_DIR + "/" + _DEV + "/" + _DAY_DIR + "/"
|
|
for filename in os.listdir(_FULL_PATH):
|
|
if _FILTER:
|
|
if _PATERN in filename:
|
|
pass
|
|
else:
|
|
continue
|
|
if filename.endswith(".MP4") and filename.startswith("G"):
|
|
myCmd = f"gopro2gpx -s {filename} {filename.replace('.MP4', '')}"
|
|
print(myCmd)
|
|
status, output = subprocess.getstatusoutput(myCmd)
|
|
print(output)
|
|
def flip_videos(_DEV,_SOURCE_DIR,_DAY_DIRS):
|
|
print("Flipping")
|
|
for _DAY_DIR in _DAY_DIRS:
|
|
if ops == "win":
|
|
_FULL_PATH = _SOURCE_DIR + "\\" + _DEV + "\\" + _DAY_DIR + "\\"
|
|
else:
|
|
_FULL_PATH = _SOURCE_DIR + "/" + _DEV + "/" + _DAY_DIR + "/"
|
|
|
|
os.chdir(_FULL_PATH)
|
|
_PROCESSED = _FULL_PATH + "processed"
|
|
if not os.path.exists(_PROCESSED):
|
|
os.mkdir(_PROCESSED)
|
|
for filename in os.listdir(_FULL_PATH):
|
|
if _FILTER:
|
|
if _PATERN in filename:
|
|
pass
|
|
else:
|
|
continue
|
|
|
|
if filename.endswith(".MP4") and not filename.startswith("joined_"):
|
|
|
|
|
|
create_date = filedate.File(filename).get()
|
|
print(filename)
|
|
export_name = 'flipped_' + filename
|
|
if ops == "win":
|
|
myCmd = [
|
|
"ffmpeg", "-i", filename ,"-c", "copy", "-metadata:s:v:0", "rotate=180", "-y", export_name
|
|
]
|
|
else:
|
|
# myCmd = [
|
|
# "/usr/bin/ffmpeg", "-i", filename, "-c", "copy", "-metadata:s:v:0", "rotate=180", "-y", export_name
|
|
# ]
|
|
# myCmd = [
|
|
# "/usr/bin/ffmpeg", "-i", filename, "-vf", "transpose=2,transpose=2", "-y", export_name
|
|
# ]
|
|
# myCmd = [
|
|
# "/usr/bin/ffmpeg", "-i", filename, "-vf", "hflip", "-c:a", "-codec", "copy", "-y", export_name
|
|
# ]
|
|
myCmd = f"exiftool -rotation=180 {filename}"
|
|
#Path = "~/Documents/File.txt"
|
|
|
|
# filedate.File(Path).set(
|
|
# created = "1st February 2003, 12:30",
|
|
# modified = "3:00 PM, 04 May 2009",
|
|
# accessed = "08/07/2014 18:30:45"
|
|
# )
|
|
if _TEST:
|
|
print(_FULL_PATH)
|
|
print(myCmd)
|
|
print(myCmd)
|
|
status, output = subprocess.getstatusoutput(myCmd)
|
|
print(output)
|
|
#proc = subprocess.Popen(myCmd,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
|
|
|
|
# print(f"Fliping {filename} in {_DAY_DIR}...".ljust(100))
|
|
# ff = FfmpegProgress(myCmd)
|
|
# with tqdm(total=100, position=1, desc="Fliping") as pbar:
|
|
# for progress in ff.run_command_with_progress():
|
|
# pbar.update(progress - pbar.n)
|
|
# pbar.close()
|
|
# get the output
|
|
#print(ff.stderr)
|
|
#ffmpeg -i .\GH010149.MP4 -c:v hevc_amf -rc cqp -qp_i 42 -qp_p 42 -quality quality -crf 25 output7.mp4
|
|
try:
|
|
shutil.move(filename + "_original", _PROCESSED)
|
|
# shutil.move(export_name, export_name)
|
|
|
|
filedate.File(filename).set(
|
|
created = create_date["created"],
|
|
modified = create_date["modified"],
|
|
accessed = create_date["accessed"]
|
|
)
|
|
except ValueError as e:
|
|
print(e)
|
|
print(f"Failed to move {export_name}")
|
|
|
|
def join_videos(_DEV,_SOURCE_DIR,_DAY_DIRS):
|
|
for _DAY_DIR in _DAY_DIRS:
|
|
if ops == "win":
|
|
_FULL_PATH = _SOURCE_DIR + "\\" + _DEV + "\\" + _DAY_DIR + "\\"
|
|
else:
|
|
_FULL_PATH = _SOURCE_DIR + _DEV + "/" + _DAY_DIR + "/"
|
|
video_seq = {}
|
|
|
|
if os.path.exists(_FULL_PATH):
|
|
if os.path.exists(_FULL_PATH + "100GOPRO"):
|
|
|
|
if ops == "win":
|
|
_FULL_PATH = _FULL_PATH + "100GOPRO\\"
|
|
else:
|
|
_FULL_PATH = _FULL_PATH + "100GOPRO/"
|
|
for filename in os.listdir(_FULL_PATH):
|
|
|
|
if filename.endswith(".MP4") and not filename.startswith("joined_"):
|
|
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}")
|
|
os.chdir(_FULL_PATH)
|
|
for vid in video_seq:
|
|
|
|
if len(_SEQUENCES) != 0 and vid not in _SEQUENCES:
|
|
continue
|
|
export_name = "joining_" + vid + ".MP4"
|
|
parts = video_seq[vid]
|
|
|
|
parts.sort()
|
|
# print(parts)
|
|
if len(parts) == 1:
|
|
shutil.move(parts[0], export_name.replace("joining","joined"))
|
|
try:
|
|
shutil.move(parts[0].replace("MP4", "THM"), _PROCESSED)
|
|
except:
|
|
print(f"Failed to move {parts[0].replace("MP4", "THM")}")
|
|
|
|
try:
|
|
shutil.move(parts[0].replace("MP4", "LRV").replace("GH","GL"), _PROCESSED)
|
|
except:
|
|
print(f"Failed to move {parts[0].replace("MP4", "LRV")}")
|
|
continue
|
|
# print(type(parts))
|
|
#ffmpeg -i concat:"input1.mp4|input2.mp4" output.mp4
|
|
|
|
|
|
f = open("video.txt", "w")
|
|
f.write("# Files to join\n")
|
|
f.close()
|
|
f = open("video.txt", "a")
|
|
final_size = 0
|
|
create_date = ""
|
|
for p in parts:
|
|
if create_date == "":
|
|
create_date = filedate.File(_FULL_PATH + p).get()
|
|
f.write(f"file '{p}'\n")
|
|
file_stats = os.stat(p)
|
|
final_size += file_stats.st_size
|
|
f.close()
|
|
#print(final_size)
|
|
#print(_FULL_PATH)
|
|
|
|
if _TEST:
|
|
input(" ???? ")
|
|
|
|
# print(os.getcwd())
|
|
#status, output = subprocess.getstatusoutput(myCmd)
|
|
if ops == "win":
|
|
myCmd = [
|
|
"ffmpeg", "-f", "concat", "-i", "video.txt","-vcodec", "copy", "-acodec", "copy", "-y", export_name
|
|
]
|
|
else:
|
|
myCmd = [
|
|
"/usr/bin/ffmpeg", "-f", "concat", "-i", "video.txt","-c", "copy", "-y", export_name
|
|
]
|
|
print(myCmd)
|
|
d_print(" ".join(myCmd))
|
|
d_print(_FULL_PATH)
|
|
print(f"Joining {len(parts)} files to {export_name} in {_DAY_DIR}...".ljust(100))
|
|
try:
|
|
os.remove(export_name)
|
|
except:
|
|
pass
|
|
os.chdir(_FULL_PATH)
|
|
print(_FULL_PATH)
|
|
print(os.getcwd())
|
|
proc = subprocess.Popen(myCmd,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
|
|
#proc = subprocess.Popen(myCmd,stderr=subprocess.STDOUT)
|
|
# with tqdm(total=100, position=1, desc="Convert") as pbar:
|
|
# for progress in ff.run_command_with_progress():
|
|
# pbar.update(progress - pbar.n)
|
|
|
|
cur_stat = 0
|
|
d_print("Running commad2")
|
|
with tqdm(total=100, position=1, desc="Join") as pbar:
|
|
position=0
|
|
while proc.returncode is None:
|
|
|
|
#pbar.update()
|
|
try:
|
|
file_stats = os.stat(export_name)
|
|
cur_size = file_stats.st_size
|
|
final_size
|
|
cur_stat = (cur_size / final_size) * 100
|
|
|
|
pbar.update(int(cur_stat) - position)
|
|
position = int(cur_stat)
|
|
proc.poll()
|
|
except:
|
|
pass
|
|
|
|
time.sleep(3)
|
|
|
|
pbar.update(1)
|
|
#pbar.close()
|
|
print("\n")
|
|
file_stats = os.stat(export_name)
|
|
print(f"Exported size: {str(file_stats.st_size )}")
|
|
print(f"Expected size: {final_size}")
|
|
if cur_stat < 99:
|
|
print("Something is wrong")
|
|
if final_size + 1000000 < final_size:
|
|
print("Something is wrong")
|
|
else:
|
|
print("Size OK")
|
|
try:
|
|
shutil.move(export_name, export_name.replace("joining","joined"))
|
|
filedate.File(_FULL_PATH + export_name.replace("joining","joined")).set(
|
|
created = create_date["created"],
|
|
modified = create_date["modified"],
|
|
accessed = create_date["accessed"]
|
|
)
|
|
except:
|
|
print(f"Failed to move {export_name}")
|
|
print("\r\n",flush=True)
|
|
print(f"\rProcess finished with rc: {proc.returncode}",flush=True)
|
|
# get the output
|
|
status = 0
|
|
|
|
if status == 0 and not _TEST:
|
|
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")}")
|
|
try:
|
|
os.remove("video.txt")
|
|
except:
|
|
pass
|
|
|
|
def encode_videos(_DEV,_SOURCE_DIR,_DAY_DIRS):
|
|
print("Encode Job")
|
|
for _DAY_DIR in _DAY_DIRS:
|
|
if ops == "win":
|
|
_FULL_PATH = _SOURCE_DIR + "\\" + _DEV + "\\" + _DAY_DIR + "\\"
|
|
else:
|
|
_FULL_PATH = _SOURCE_DIR + "/" + _DEV + "/" + _DAY_DIR + "/"
|
|
if os.path.exists(_FULL_PATH):
|
|
if os.path.exists(_FULL_PATH + "100GOPRO"):
|
|
_FULL_PATH = _FULL_PATH + "100GOPRO"
|
|
parts = []
|
|
for filename in os.listdir(_FULL_PATH):
|
|
if filename.startswith("joined_") and filename.endswith(".MP4"):
|
|
parts.append(filename)
|
|
else:
|
|
sys.exit()
|
|
|
|
os.chdir(_FULL_PATH)
|
|
create_date = ""
|
|
|
|
codec = "hw"
|
|
for p in parts:
|
|
if create_date == "":
|
|
create_date = filedate.File(_FULL_PATH + p).get()
|
|
export_name = "encoding_" + p
|
|
if ops == "win":
|
|
myCmd = [
|
|
"ffmpeg", "-i", p, "-c:v", "hevc_amf", "-rc", "cqp", "-qp_i", _QUALITY[0], "-qp_p", _QUALITY[1], "-quality", "quality", "-crf", _QUALITY[2], "-y", export_name , "-f", "null", "/dev/null",
|
|
]
|
|
else:
|
|
myCmd = [
|
|
"ffmpeg", "-vaapi_device", "/dev/dri/renderD128", "-i", p, "-vf", "format=nv12,hwupload", "-c:v", "hevc_vaapi", "-b:v", "8M", "-c:a", "copy", "-y", export_name
|
|
]
|
|
print(" ".join(myCmd))
|
|
d_print(" ".join(myCmd))
|
|
print(f"Encoding {p} to {export_name} in {_DAY_DIR}...".ljust(100))
|
|
ff = FfmpegProgress(myCmd)
|
|
with tqdm(total=100, position=1, desc="Encode") as pbar:
|
|
for progress in ff.run_command_with_progress():
|
|
pbar.update(progress - pbar.n)
|
|
pbar.close()
|
|
# get the output
|
|
#print(ff.stderr)
|
|
#ffmpeg -i .\GH010149.MP4 -c:v hevc_amf -rc cqp -qp_i 42 -qp_p 42 -quality quality -crf 25 output7.mp4
|
|
try:
|
|
shutil.move(export_name, export_name.replace("encoding","encoded"))
|
|
filedate.File(_FULL_PATH + export_name.replace("encoding","encoded")).set(
|
|
created = create_date["created"],
|
|
modified = create_date["modified"],
|
|
accessed = create_date["accessed"]
|
|
)
|
|
except:
|
|
print(f"Failed to move {export_name}")
|
|
|
|
|
|
|
|
if _ACTION == "modify_date":
|
|
modify_creation_date(_FILE,_SRC_FILE)
|
|
|
|
|
|
if _ACTION == "flip":
|
|
flip_videos(_DEV, _SOURCE_DIR,_DAY_DIRS)
|
|
|
|
if _ACTION == "join":
|
|
join_videos(_DEV, _SOURCE_DIR,_DAY_DIRS)
|
|
|
|
if _ACTION == "encode":
|
|
encode_videos(_DEV, _SOURCE_DIR,_DAY_DIRS)
|
|
|
|
if _ACTION == "all":
|
|
flip_videos(_DEV, _SOURCE_DIR,_DAY_DIRS)
|
|
join_videos(_DEV, _SOURCE_DIR,_DAY_DIRS)
|
|
if _ACTION == "get_metadata":
|
|
modify_creation_date("/media/nas/nas-photo/imported/gopro9_1/2025-06-02", "GH010256.MP4")
|
|
if _ACTION == "export_gps":
|
|
export_gps(_DEV, _SOURCE_DIR,_DAY_DIRS) |