Skip to content

Commit

Permalink
Create timelapse movies using specified codec instead of forcing avi.…
Browse files Browse the repository at this point in the history
… See upstream PR #2110
  • Loading branch information
Mictronics committed Dec 29, 2021
1 parent f5fba42 commit c94863d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 3 additions & 2 deletions motioneye/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,9 +1279,10 @@ def timelapse(self, camera_id, group):

pretty_filename = camera_config['camera_name'] + '_' + group
pretty_filename = re.sub('[^a-zA-Z0-9]', '_', pretty_filename)
pretty_filename += '.' + mediafiles.FFMPEG_EXT_MAPPING.get(camera_config['movie_codec'], 'avi')
filename_ext = mediafiles.FFMPEG_EXT_MAPPING.get(camera_config['movie_codec'], 'avi')
pretty_filename += '.' + filename_ext

self.set_header('Content-Type', 'video/x-msvideo')
self.set_header('Content-Type', mediafiles.MOVIE_EXT_TYPE_MAPPING.get(filename_ext, 'video/x-msvideo'))
self.set_header('Content-Disposition', 'attachment; filename=' + pretty_filename + ';')
self.finish(data)

Expand Down
24 changes: 19 additions & 5 deletions motioneye/mediafiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@
'hevc': 'mp4'
}

MOVIE_EXT_TYPE_MAPPING = {
'avi': 'video/x-msvideo',
'mp4': 'video/mp4',
'mov': 'video/quicktime',
'swf': 'application/x-shockwave-flash',
'flv': 'video/x-flv',
'mkv': 'video/x-matroska'
}

# a cache of prepared files (whose preparing time is significant)
_prepared_files = {}

Expand Down Expand Up @@ -592,10 +601,12 @@ def make_timelapse_movie(camera_config, framerate, interval, group):
global _timelapse_data

target_dir = camera_config.get('target_dir')
codec = camera_config.get('movie_codec')
# save movie_codec as a different variable so it doesn't get lost in the CODEC_MAPPING
movie_codec = camera_config.get('movie_codec')

codec = FFMPEG_CODEC_MAPPING.get(codec, codec)
fmt = FFMPEG_FORMAT_MAPPING.get(codec, codec)
codec = FFMPEG_CODEC_MAPPING.get(movie_codec, movie_codec)
fmt = FFMPEG_FORMAT_MAPPING.get(movie_codec, movie_codec)
file_format = FFMPEG_EXT_MAPPING.get(movie_codec, movie_codec)

# create a subprocess to retrieve media files
def do_list_media(pipe):
Expand Down Expand Up @@ -625,7 +636,9 @@ def do_list_media(pipe):
started = [datetime.datetime.now()]
media_list = []

tmp_filename = os.path.join(settings.MEDIA_PATH, '.%s.avi' % int(time.time()))
# use correct extension for the movie_codec
tmp_filename = os.path.join(settings.MEDIA_PATH, '.%(name)s.%(ext)s')
tmp_filename = tmp_filename % { 'name': int(time.time()), 'ext': file_format }

def read_media_list():
while parent_pipe.poll():
Expand Down Expand Up @@ -694,9 +707,10 @@ def select_pictures(media_list):
def make_movie(pictures):
global _timelapse_process

# don't specify file format with -f, let ffmpeg work it out from the extension
cmd = 'rm -f %(tmp_filename)s;'
cmd += 'cat %(jpegs)s | ffmpeg -framerate %(framerate)s -f image2pipe -vcodec mjpeg -i - -vcodec %(codec)s ' \
'-format %(format)s -b:v %(bitrate)s -qscale:v 0.1 -f avi %(tmp_filename)s'
'-format %(format)s -b:v %(bitrate)s -qscale:v 0.1 %(tmp_filename)s'

bitrate = 9999999

Expand Down

0 comments on commit c94863d

Please sign in to comment.