From c4bacbc7b83f3d35082ec0539f398743f687e13d Mon Sep 17 00:00:00 2001 From: Lukas Schauer <lukas@schauer.so> Date: Thu, 30 Apr 2020 00:43:48 +0200 Subject: [PATCH] added hidden notranscode option for video uploads that already are h264+aac and only need to be remuxed --- videos/api.py | 1 + videos/migrations/0002_video_notranscode.py | 18 ++++++++++ videos/models.py | 1 + videos/templates/worker.py | 40 +++++++++++++-------- videos/views.py | 1 + 5 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 videos/migrations/0002_video_notranscode.py diff --git a/videos/api.py b/videos/api.py index 2f100bb..96cea59 100755 --- a/videos/api.py +++ b/videos/api.py @@ -43,6 +43,7 @@ def task(request): task['type'] = 'master' task['course'] = video.course.id task['video'] = video.id + task['notranscode'] = video.notranscode task['path'] = str(video.original) return JsonResponse(task) diff --git a/videos/migrations/0002_video_notranscode.py b/videos/migrations/0002_video_notranscode.py new file mode 100644 index 0000000..972d9cf --- /dev/null +++ b/videos/migrations/0002_video_notranscode.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.5 on 2020-04-29 21:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('videos', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='video', + name='notranscode', + field=models.BooleanField(default=False), + ), + ] diff --git a/videos/models.py b/videos/models.py index 368cd4c..9f54adc 100755 --- a/videos/models.py +++ b/videos/models.py @@ -155,6 +155,7 @@ class Video(MetaVideo): course = models.ForeignKey('Course', Course, null=False, blank=False) key = models.CharField(blank=False, null=False, default=random_key_20, max_length=30, unique=True) views = models.IntegerField(default=0) + notranscode = models.BooleanField(default=False) name = models.CharField(max_length=1000, blank=True) description = models.TextField(blank=True) length = models.DecimalField(null=True, blank=True, decimal_places=3, max_digits=10) diff --git a/videos/templates/worker.py b/videos/templates/worker.py index 0103846..4a7890c 100644 --- a/videos/templates/worker.py +++ b/videos/templates/worker.py @@ -77,21 +77,31 @@ def render_master(task, tmpfiles=[]): ffmpeg_command = ['ffmpeg', '-y', '-v', 'info', '-nostats'] ffmpeg_command += ['-analyzeduration', '20000000'] ffmpeg_command += ['-i', tmpfiles[0]] - filters = ['[0:v:0] hqdn3d,pad=ceil(iw/2)*2:ceil(ih/2)*2 [v]'] - - for i in range(len(audio_streams)): - filters += ["[0:a:{i}] dynaudnorm='p=0.35:r=1:f=300',loudnorm='i=-16.0:lra=12.0:tp=-3.0',anull [amix_{i}]".format(i=i)] - if len(video_streams) == 2: - filters += ['[0:v:1] pad=ceil(iw/2)*2:ceil(ih/2)*2 [s]'] - ffmpeg_command += ['-filter_complex', '; '.join(filters)] - for i in range(len(audio_streams)): - ffmpeg_command += ['-map', '[amix_{}]'.format(i)] - ffmpeg_command += ['-c:a:{}'.format(i), 'aac', '-b:a:{}'.format(i), '128k', '-ar:a:{}'.format(i), '48000'] - ffmpeg_command += ['-map', '[v]'] - ffmpeg_command += ['-c:v:0', 'libx264', '-threads', '4', '-pix_fmt', 'yuv420p', '-crf:v:0', '23', '-profile:v:0', 'high', '-level:v:0', '4.1', '-disposition:v:0', 'default', '-metadata:s:v:0', 'title="Video"'] - if len(video_streams) == 2: - ffmpeg_command += ['-map', '[s]'] - ffmpeg_command += ['-c:v:1', 'libx264', '-threads', '4', '-pix_fmt', 'yuv420p', '-crf:v:1', '23', '-profile:v:1', 'high', '-level:v:1', '4.1', '-tune:v:1', 'stillimage', '-disposition:v:1', 'descriptions', '-metadata:s:v:1', 'title="Slides"'] + + if task['notranscode']: + log(task, "notranscode flag is set, only remuxing video+audio") + ffmpeg_command += ['-c:v:0', 'copy', '-map', '0:v:0'] + if len(video_streams) == 2: + ffmpeg_command += ['-c:v:1', 'copy', '-map', '0:v:1'] + for i in range(len(audio_streams)): + ffmpeg_command += ['-c:a:%d' % i, 'copy', '-map', '0:a:%d' % i] + else: + log(task, "notranscode flag is not set, transcoding video+audio") + filters = ['[0:v:0] hqdn3d,pad=ceil(iw/2)*2:ceil(ih/2)*2 [v]'] + + for i in range(len(audio_streams)): + filters += ["[0:a:{i}] dynaudnorm='p=0.35:r=1:f=300',loudnorm='i=-16.0:lra=12.0:tp=-3.0',anull [amix_{i}]".format(i=i)] + if len(video_streams) == 2: + filters += ['[0:v:1] pad=ceil(iw/2)*2:ceil(ih/2)*2 [s]'] + ffmpeg_command += ['-filter_complex', '; '.join(filters)] + for i in range(len(audio_streams)): + ffmpeg_command += ['-map', '[amix_{}]'.format(i)] + ffmpeg_command += ['-c:a:{}'.format(i), 'aac', '-b:a:{}'.format(i), '128k', '-ar:a:{}'.format(i), '48000'] + ffmpeg_command += ['-map', '[v]'] + ffmpeg_command += ['-c:v:0', 'libx264', '-threads', '4', '-pix_fmt', 'yuv420p', '-crf:v:0', '23', '-profile:v:0', 'high', '-level:v:0', '4.1', '-disposition:v:0', 'default', '-metadata:s:v:0', 'title="Video"'] + if len(video_streams) == 2: + ffmpeg_command += ['-map', '[s]'] + ffmpeg_command += ['-c:v:1', 'libx264', '-threads', '4', '-pix_fmt', 'yuv420p', '-crf:v:1', '23', '-profile:v:1', 'high', '-level:v:1', '4.1', '-tune:v:1', 'stillimage', '-disposition:v:1', 'descriptions', '-metadata:s:v:1', 'title="Slides"'] # ffmpeg_command += ['-aspect', '16:9'] ffmpeg_command += ['-movflags', 'faststart', '-f', 'mp4', tmpfiles[1]] log(task, repr(ffmpeg_command)) diff --git a/videos/views.py b/videos/views.py index 1bd49fd..50602df 100755 --- a/videos/views.py +++ b/videos/views.py @@ -215,6 +215,7 @@ def upload(request): v.name = request.POST.get("name").strip() v.description = request.POST.get("description").strip() v.original = request.FILES.get('videofile') + v.notranscode = (request.POST.get("notranscode") == "1") if not v.original: return HttpResponse("File missing") v.save() -- GitLab