Skip to content

Commit

Permalink
process uploads folder and imports every video and photo, able to dow…
Browse files Browse the repository at this point in the history
…nload a single file with all videos joined and omre
  • Loading branch information
elblogbruno committed Aug 8, 2021
1 parent 1749821 commit 41e0e16
Show file tree
Hide file tree
Showing 51 changed files with 656 additions and 248 deletions.
Binary file added 20190417_193010_1TEMP_MPY_wvf_snd.mp3
Binary file not shown.
9 changes: 8 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from config import UPLOAD_FOLDER
from controller import create_app
from flask_sqlalchemy import SQLAlchemy
import db
from controller.models.models import Wanderpi
from controller import socketio
from controller.utils.watcher import ImagesWatcher
import threading

app = create_app('dev')

if __name__ == '__main__':
#app.run(threaded=True, host="0.0.0.0")
db.Base.metadata.create_all(db.engine)
#app.run(threaded=True, host="0.0.0.0", port=5000)
socketio.run(threaded=True, host="0.0.0.0", ssl_context='adhoc')

app.run(threaded=True, host="0.0.0.0", ssl_context='adhoc')
# w = threading.Thread(target=ImagesWatcher(UPLOAD_FOLDER).run())

# w.start()
# t.start()
Binary file added c6f5d6bc-8f69-46af-b703-4e08df41f0e0.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

UPLOAD_FOLDER = os.getcwd()+'/uploads/'
STATIC_FOLDER = os.getcwd()+'/controller/static/'
VIDEOS_FOLDER = os.getcwd()+'/controller/static/videos'
VIDEOS_FOLDER = os.getcwd()+'/controller/static/wanderpis/'


class Config:
Expand All @@ -13,7 +13,7 @@ class Config:
# session加密秘钥
SECRET_KEY = "fM3PEZwSRcbLkk2Ew82yZFffdAYsNgOddWoANdQo/U3VLZ/qNsOKLsQPYXDPon2t"

MAX_CONTENT_LENGTH = 100 * 1024 * 1024
MAX_CONTENT_LENGTH = 200 * 1024 * 1024
# session过期时间
PERMANENT_SESSION_LIFETIME = timedelta(days=7)

Expand Down
58 changes: 49 additions & 9 deletions controller/models/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from werkzeug.security import generate_password_hash, check_password_hash
import db
from sqlalchemy import Column, Boolean, String, Float, DateTime, Text, Date, Time
from sqlalchemy import Column, Boolean, String, Float, DateTime, Text, Date, Time, desc
from datetime import datetime
import os
import json
import shutil

def datetime_parser(o):
if isinstance(o, datetime):
return o.__str__()
class Wanderpi(db.Base):
__tablename__ = 'wanderpi'
id = Column(String(256), primary_key=True)
Expand All @@ -16,6 +22,7 @@ class Wanderpi(db.Base):
created_date = Column(DateTime, default=datetime.utcnow)
travel_id = Column(String(256), nullable=False)
is_image = Column(Boolean)
has_been_edited = Column(Boolean, default=False)

def __repr__(self):
return f'<User {self.id}>'
Expand All @@ -29,10 +36,25 @@ def set_name(self, id):
def save(self):
db.session.add(self)
db.session.commit()
#self.save_json()

@staticmethod
def delete(id):
db.session.query(Wanderpi).filter(Wanderpi.id == id).delete()
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}

# this functions gets the object as a dictionary and saves it to a json file
def save_json(self):
#write json file on path where file is located
json_file_path = "./controller"+ os.path.dirname(self.file_path) + '/{0}.json'.format(self.id)
print(json_file_path)
with open(json_file_path, 'w') as f:
json.dump(self.as_dict(), f, default = datetime_parser)

def delete(self):
if os.path.isfile(self.file_path):
print("Removing file from disk")
os.remove(self.file_path)

db.session.query(Wanderpi).filter(Wanderpi.id == self.id).delete()
db.session.commit()
return True

Expand All @@ -52,6 +74,7 @@ class Travel(db.Base):
name = Column(String(256), nullable=False)
lat = Column(String(256), nullable=False)
long = Column(String(256), nullable=False)
travel_folder_path = Column(String(256), nullable=False)
destination = Column(String(256), nullable=False)
created_date = Column(DateTime, default=datetime.utcnow)
start_date = Column(Date)
Expand All @@ -71,23 +94,40 @@ def save(self):
print(self.start_date)
db.session.add(self)
db.session.commit()
self.save_json()

def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
# this functions gets the object as a dictionary and saves it to a json file
def save_json(self):
#write json file on path where file is located
json_file_path = self.travel_folder_path + '/{0}.json'.format(self.id)

with open(json_file_path, 'w') as f:
json.dump(self.as_dict(), f, default = datetime_parser)

def delete_all_wanderpis(self):
video = db.session.query(Wanderpi).filter(Wanderpi.travel_id == self.id).all()
for v in video:
db.session.delete(v)
videos = db.session.query(Wanderpi).filter(Wanderpi.travel_id == self.id).all()

for video in videos:
video.delete()

db.session.commit()
return True

def get_all_wanderpis(self):
return db.session.query(Wanderpi).filter(Wanderpi.travel_id == self.id).all()

@staticmethod
def delete(id):
def delete(self, id):
if os.path.isdir(self.travel_folder_path):
print("Removing folder from disk")
shutil.rmtree(self.travel_folder_path, ignore_errors=True)

db.session.query(Travel).filter(Travel.id == id).delete()
db.session.commit()
return True


@staticmethod
def get_by_id(id):
return db.session.query(Travel).get(id)
Expand Down
51 changes: 51 additions & 0 deletions controller/modules/files/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import exifread
from datetime import datetime


STATIC_FOLDER = '/static/wanderpis/'
VIDEO_EXTENSIONS = set(['mp4'])
IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
# Allowed extension you can set your own
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'mp4'])

def get_file_extension(filename):
if '.' in filename:
return filename.rsplit('.', 1)[1].lower()
else:
return 'mp4'

def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

#convert degree minute second to degree decimal
def dms_to_dd(dms):
dd = float(dms[0]) + float(dms[1])/60 + float(dms[2])/3600
return float(dd)

def get_lat_long_tags(path_name):
f = open(path_name, 'rb')

tags = exifread.process_file(f, stop_tag='GPS')

lat = 0
long = 0

for tag in tags.keys():
if tag == 'GPS GPSLatitude':
lat = dms_to_dd(tags[tag].values)
elif tag == 'GPS GPSLongitude':
long = dms_to_dd(tags[tag].values)

if lat == 0 and long == 0:
lat = 0
long = 0

return lat, long

def create_folder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print('Error: Creating directory. ' + directory)
28 changes: 17 additions & 11 deletions controller/modules/files/video_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random
import moviepy.editor as mp
import cv2

class VideoUtils:
@staticmethod
def get_video_info(path):
Expand All @@ -11,19 +11,25 @@ def get_video_info(path):
#vid_info = "dsds"
return mp.VideoFileClip(path).duration

# An example of abs file path is: ./controller/static/wanderpis/{travel-id}/thumbnail/thumbnail-{video-id}.jpg
@staticmethod
def get_video_thumbnail(path, video_id):
vcap = cv2.VideoCapture(path)
res, im_ar = vcap.read()
while im_ar.mean() < 1 and res:
res, im_ar = vcap.read()
def save_video_thumbnail(file_path, abs_file_path, video_id):
video = cv2.VideoCapture(file_path)

total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))

#get random number between 0 and total_frames
frame_number = random.randint(0, total_frames)

video.set(1, frame_number)
ret, frame = video.read()

#im_ar = cv2.resize(im_ar, (thumb_width, thumb_height), 0, 0, cv2.INTER_LINEAR)
#to save we have two options
#1) save on a file
thumbnail_url = "./controller/static/thumbnails/thumbnail-%s.jpg" % str(video_id)

cv2.imwrite(thumbnail_url, im_ar)

return "/static/thumbnails/thumbnail-%s.jpg" % str(video_id)
thumbnail_url = abs_file_path + "/thumbnail-%s.jpg" % str(video_id)
if ret:
cv2.imwrite(thumbnail_url, frame)
else:
print("Error reading video file")

Loading

0 comments on commit 41e0e16

Please sign in to comment.