Skip to content

Commit

Permalink
refactor: blackify :)
Browse files Browse the repository at this point in the history
  • Loading branch information
escaped committed Oct 8, 2020
1 parent fba2601 commit c2c14e0
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 92 deletions.
20 changes: 16 additions & 4 deletions test_proj/media_library/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@

class Migration(migrations.Migration):

dependencies = [
]
dependencies = []

operations = [
migrations.CreateModel(
name='Video',
fields=[
('id', models.AutoField(primary_key=True, serialize=False, auto_created=True, verbose_name='ID')),
(
'id',
models.AutoField(
primary_key=True,
serialize=False,
auto_created=True,
verbose_name='ID',
),
),
('width', models.PositiveIntegerField(editable=False, default=0)),
('height', models.PositiveIntegerField(editable=False, default=0)),
('duration', models.FloatField(editable=False, default=0)),
('file', video_encoding.fields.VideoField(width_field='width', height_field='height', upload_to='')),
(
'file',
video_encoding.fields.VideoField(
width_field='width', height_field='height', upload_to=''
),
),
],
),
]
7 changes: 4 additions & 3 deletions test_proj/media_library/tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def test_encoding(video):

assert video.format_set.count() == 4

formats = dict([(o['name'], o)
for o in settings.VIDEO_ENCODING_FORMATS['FFmpeg']])
assert set(video.format_set.values_list('format', flat=True)) == set(formats.keys()) # NOQA
formats = dict([(o['name'], o) for o in settings.VIDEO_ENCODING_FORMATS['FFmpeg']])
assert set(video.format_set.values_list('format', flat=True)) == set(
formats.keys()
) # NOQA

for f in video.format_set.all():
assert formats[f.format]['extension'] == f.file.name.split('.')[-1]
Expand Down
22 changes: 11 additions & 11 deletions test_proj/media_library/tests/test_ffmpeg_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
def test_get_media_info(ffmpeg, video_path):
media_info = ffmpeg.get_media_info(video_path)

assert media_info == {'width': 1280,
'height': 720,
'duration': 2.022}
assert media_info == {'width': 1280, 'height': 720, 'duration': 2.022}


def test_encode(ffmpeg, video_path):
__, target_path = tempfile.mkstemp(suffix='.mp4')
encoding = ffmpeg.encode(video_path, target_path, ['-vf', 'scale=-2:320',
'-r', '20',
'-codec:v', 'libx264'])
encoding = ffmpeg.encode(
video_path,
target_path,
['-vf', 'scale=-2:320', '-r', '20', '-codec:v', 'libx264'],
)
percent = next(encoding)
assert 0 <= percent <= 100
while percent:
Expand All @@ -33,9 +33,7 @@ def test_encode(ffmpeg, video_path):
assert percent == 100
assert os.path.isfile(target_path)
media_info = ffmpeg.get_media_info(target_path)
assert media_info == {'width': 568,
'height': 320,
'duration': 2.1}
assert media_info == {'width': 568, 'height': 320, 'duration': 2.1}


def test_get_thumbnail(ffmpeg, video_path):
Expand All @@ -54,7 +52,8 @@ def test_get_thumbnail__invalid_time(ffmpeg, video_path):


@pytest.mark.parametrize(
'offset', (0, 0.02),
'offset',
(0, 0.02),
)
def test_get_thumbnail__too_close_to_the_end(ffmpeg, video_path, offset):
"""
Expand All @@ -65,7 +64,8 @@ def test_get_thumbnail__too_close_to_the_end(ffmpeg, video_path, offset):

with pytest.raises(exceptions.InvalidTimeError):
ffmpeg.get_thumbnail(
video_path, at_time=duration - offset,
video_path,
at_time=duration - offset,
)


Expand Down
5 changes: 4 additions & 1 deletion test_proj/media_library/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ def video_format(video):
return Format.objects.create(
object_id=video.pk,
content_type=ContentType.objects.get_for_model(video),
field_name='file', format='mp4_hd', progress=100)
field_name='file',
format='mp4_hd',
progress=100,
)


@pytest.mark.django_db
Expand Down
1 change: 0 additions & 1 deletion test_proj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'video_encoding',
'test_proj.media_library',
)
Expand Down
4 changes: 3 additions & 1 deletion video_encoding/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def get_backend_class():
except ImportError as e:
raise ImproperlyConfigured(
_("Cannot retrieve backend '{}'. Error: '{}'.").format(
settings.VIDEO_ENCODING_BACKEND, e))
settings.VIDEO_ENCODING_BACKEND, e
)
)
return cls


Expand Down
71 changes: 47 additions & 24 deletions video_encoding/backends/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,66 @@ def __init__(self):
'-threads',
str(settings.VIDEO_ENCODING_THREADS),
'-y', # overwrite temporary created file
'-strict', '-2', # support aac codec (which is experimental)
'-strict',
'-2', # support aac codec (which is experimental)
]

self.ffmpeg_path = getattr(
settings, 'VIDEO_ENCODING_FFMPEG_PATH', which('ffmpeg'))
settings, 'VIDEO_ENCODING_FFMPEG_PATH', which('ffmpeg')
)
self.ffprobe_path = getattr(
settings, 'VIDEO_ENCODING_FFPROBE_PATH', which('ffprobe'))
settings, 'VIDEO_ENCODING_FFPROBE_PATH', which('ffprobe')
)

if not self.ffmpeg_path:
raise exceptions.FFmpegError("ffmpeg binary not found: {}".format(
self.ffmpeg_path or ''))
raise exceptions.FFmpegError(
"ffmpeg binary not found: {}".format(self.ffmpeg_path or '')
)

if not self.ffprobe_path:
raise exceptions.FFmpegError("ffprobe binary not found: {}".format(
self.ffmpeg_path or ''))
raise exceptions.FFmpegError(
"ffprobe binary not found: {}".format(self.ffmpeg_path or '')
)

@classmethod
def check(cls):
errors = super(FFmpegBackend, cls).check()
try:
FFmpegBackend()
except exceptions.FFmpegError as e:
errors.append(checks.Error(
e.msg,
hint="Please install ffmpeg.",
obj=cls,
id='video_conversion.E001',
))
errors.append(
checks.Error(
e.msg,
hint="Please install ffmpeg.",
obj=cls,
id='video_conversion.E001',
)
)
return errors

def _spawn(self, cmds):
try:
return Popen(
cmds, shell=False,
stdin=PIPE, stdout=PIPE, stderr=PIPE,
cmds,
shell=False,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
close_fds=True,
)
except OSError as e:
raise six.raise_from(
exceptions.FFmpegError('Error while running ffmpeg binary'), e)
exceptions.FFmpegError('Error while running ffmpeg binary'), e
)

def _check_returncode(self, process):
stdout, stderr = process.communicate()
if process.returncode != 0:
raise exceptions.FFmpegError("`{}` exited with code {:d}".format(
' '.join(process.args), process.returncode))
raise exceptions.FFmpegError(
"`{}` exited with code {:d}".format(
' '.join(process.args), process.returncode
)
)
self.stdout = stdout.decode(console_encoding)
self.stderr = stderr.decode(console_encoding)
return self.stdout, self.stderr
Expand Down Expand Up @@ -140,12 +154,21 @@ def encode(self, source_path, target_path, params): # NOQA: C901

def _parse_media_info(self, data):
media_info = json.loads(data)
media_info['video'] = [stream for stream in media_info['streams']
if stream['codec_type'] == 'video']
media_info['audio'] = [stream for stream in media_info['streams']
if stream['codec_type'] == 'audio']
media_info['subtitle'] = [stream for stream in media_info['streams']
if stream['codec_type'] == 'subtitle']
media_info['video'] = [
stream
for stream in media_info['streams']
if stream['codec_type'] == 'video'
]
media_info['audio'] = [
stream
for stream in media_info['streams']
if stream['codec_type'] == 'audio'
]
media_info['subtitle'] = [
stream
for stream in media_info['streams']
if stream['codec_type'] == 'subtitle'
]
del media_info['streams']
return media_info

Expand Down
98 changes: 82 additions & 16 deletions video_encoding/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,106 @@ class VideoEncodingAppConf(AppConf):
'name': 'webm_sd',
'extension': 'webm',
'params': [
'-b:v', '1000k', '-maxrate', '1000k', '-bufsize', '2000k',
'-codec:v', 'libvpx', '-r', '30',
'-vf', 'scale=-1:480', '-qmin', '10', '-qmax', '42',
'-codec:a', 'libvorbis', '-b:a', '128k', '-f', 'webm',
'-b:v',
'1000k',
'-maxrate',
'1000k',
'-bufsize',
'2000k',
'-codec:v',
'libvpx',
'-r',
'30',
'-vf',
'scale=-1:480',
'-qmin',
'10',
'-qmax',
'42',
'-codec:a',
'libvorbis',
'-b:a',
'128k',
'-f',
'webm',
],
},
{
'name': 'webm_hd',
'extension': 'webm',
'params': [
'-codec:v', 'libvpx',
'-b:v', '3000k', '-maxrate', '3000k', '-bufsize', '6000k',
'-vf', 'scale=-1:720', '-qmin', '11', '-qmax', '51',
'-acodec', 'libvorbis', '-b:a', '128k', '-f', 'webm',
'-codec:v',
'libvpx',
'-b:v',
'3000k',
'-maxrate',
'3000k',
'-bufsize',
'6000k',
'-vf',
'scale=-1:720',
'-qmin',
'11',
'-qmax',
'51',
'-acodec',
'libvorbis',
'-b:a',
'128k',
'-f',
'webm',
],
},
{
'name': 'mp4_sd',
'extension': 'mp4',
'params': [
'-codec:v', 'libx264', '-crf', '20', '-preset', 'medium',
'-b:v', '1000k', '-maxrate', '1000k', '-bufsize', '2000k',
'-vf', 'scale=-2:480', # http://superuser.com/a/776254
'-codec:a', 'aac', '-b:a', '128k', '-strict', '-2',
'-codec:v',
'libx264',
'-crf',
'20',
'-preset',
'medium',
'-b:v',
'1000k',
'-maxrate',
'1000k',
'-bufsize',
'2000k',
'-vf',
'scale=-2:480', # http://superuser.com/a/776254
'-codec:a',
'aac',
'-b:a',
'128k',
'-strict',
'-2',
],
},
{
'name': 'mp4_hd',
'extension': 'mp4',
'params': [
'-codec:v', 'libx264', '-crf', '20', '-preset', 'medium',
'-b:v', '3000k', '-maxrate', '3000k', '-bufsize', '6000k',
'-vf', 'scale=-2:720',
'-codec:a', 'aac', '-b:a', '128k', '-strict', '-2',
'-codec:v',
'libx264',
'-crf',
'20',
'-preset',
'medium',
'-b:v',
'3000k',
'-maxrate',
'3000k',
'-bufsize',
'6000k',
'-vf',
'scale=-2:720',
'-codec:a',
'aac',
'-b:a',
'128k',
'-strict',
'-2',
],
},
]
Expand Down
11 changes: 5 additions & 6 deletions video_encoding/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db.models.fields.files import (FieldFile, ImageField,
ImageFileDescriptor)
from django.db.models.fields.files import FieldFile, ImageField, ImageFileDescriptor
from django.utils.translation import ugettext as _

from .backends import get_backend_class
Expand All @@ -23,8 +22,7 @@ class VideoField(ImageField):
descriptor_class = VideoFileDescriptor
description = _("Video")

def __init__(self, verbose_name=None, name=None, duration_field=None,
**kwargs):
def __init__(self, verbose_name=None, name=None, duration_field=None, **kwargs):
self.duration_field = duration_field
super(VideoField, self).__init__(verbose_name, name, **kwargs)

Expand All @@ -49,8 +47,9 @@ def update_dimension_fields(self, instance, force=False, *args, **kwargs):
return

# write `width` and `height`
super(VideoField, self).update_dimension_fields(instance, force,
*args, **kwargs)
super(VideoField, self).update_dimension_fields(
instance, force, *args, **kwargs
)
if not self.duration_field:
return

Expand Down
Loading

0 comments on commit c2c14e0

Please sign in to comment.