add support for audio tracks

This commit is contained in:
j 2014-07-23 17:27:27 +02:00
parent dceb917316
commit f61cc7ee54
8 changed files with 286 additions and 12 deletions

View file

@ -70,7 +70,7 @@ def avconv_version():
version = stderr.split(' ')[2].split('-')[0]
return version
def stream(video, target, profile, info, avconv=None):
def stream(video, target, profile, info, avconv=None, audio_track=0):
if not os.path.exists(target):
ox.makedirs(os.path.dirname(target))
@ -231,14 +231,36 @@ def stream(video, target, profile, info, avconv=None):
'-qmin', '10', '-qmax', '51',
'-qdiff', '4'
]
video_settings += ['-map', '0:%s,0:0'%info['video'][0]['id']]
else:
video_settings = ['-vn']
if info['audio']:
if video_settings == ['-vn'] or not info['video']:
n = 0
else:
n = 1
#mix 2 mono channels into stereo(common for fcp dv mov files)
if audio_track == 0 and len(info['audio']) == 2 \
and len(filter(None, [a['channels'] == 1 or None for a in info['audio']])) == 2:
video_settings += [
'-filter_complex',
'[0:%s][0:%s] amerge' % (info['audio'][0]['id'], info['audio'][1]['id'])
]
mono_mix = True
else:
video_settings += ['-map', '0:%s,0:%s' % (info['audio'][audio_track]['id'], n)]
mono_mix = False
audio_settings = ['-ar', str(audiorate), '-aq', str(audioquality)]
if audiochannels and 'channels' in info['audio'][0] \
and info['audio'][0]['channels'] > audiochannels:
audio_settings += ['-ac', str(audiochannels)]
if mono_mix:
ac = 2
else:
ac = info['audio'][audio_track].get('channels', audiochannels)
if ac:
ac = min(ac, audiochannels)
else:
ac = audiochannels
audio_settings += ['-ac', str(ac)]
if audiobitrate:
audio_settings += ['-ab', audiobitrate]
if format == 'mp4':

View file

@ -0,0 +1,172 @@
# -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Changing field 'File.language'
db.alter_column('archive_file', 'language', self.gf('django.db.models.fields.CharField')(max_length=255, null=True))
def backwards(self, orm):
# Changing field 'File.language'
db.alter_column('archive_file', 'language', self.gf('django.db.models.fields.CharField')(max_length=8, null=True))
models = {
'archive.file': {
'Meta': {'object_name': 'File'},
'audio_codec': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'available': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'bits_per_pixel': ('django.db.models.fields.FloatField', [], {'default': '-1'}),
'channels': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'data': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
'display_aspect_ratio': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'duration': ('django.db.models.fields.FloatField', [], {'null': 'True'}),
'encoding': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'extension': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'null': 'True'}),
'failed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'framerate': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'height': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'info': ('ox.django.fields.DictField', [], {'default': '{}'}),
'is_audio': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_subtitle': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_video': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'item': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'files'", 'null': 'True', 'to': "orm['item.Item']"}),
'language': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'null': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'oshash': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '16'}),
'part': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'null': 'True'}),
'part_title': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'null': 'True'}),
'path': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '2048'}),
'pixel_format': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'pixels': ('django.db.models.fields.BigIntegerField', [], {'default': '0'}),
'queued': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'samplerate': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'selected': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'size': ('django.db.models.fields.BigIntegerField', [], {'default': '0'}),
'sort_path': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '2048'}),
'type': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255'}),
'uploading': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'version': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'null': 'True'}),
'video_codec': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'wanted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'width': ('django.db.models.fields.IntegerField', [], {'default': '0'})
},
'archive.frame': {
'Meta': {'unique_together': "(('file', 'position'),)", 'object_name': 'Frame'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'file': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'frames'", 'to': "orm['archive.File']"}),
'frame': ('django.db.models.fields.files.ImageField', [], {'default': 'None', 'max_length': '100', 'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'position': ('django.db.models.fields.FloatField', [], {})
},
'archive.instance': {
'Meta': {'unique_together': "(('path', 'volume'),)", 'object_name': 'Instance'},
'atime': ('django.db.models.fields.IntegerField', [], {'default': '1406046216'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'ctime': ('django.db.models.fields.IntegerField', [], {'default': '1406046216'}),
'file': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'instances'", 'to': "orm['archive.File']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'ignore': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'mtime': ('django.db.models.fields.IntegerField', [], {'default': '1406046216'}),
'path': ('django.db.models.fields.CharField', [], {'max_length': '2048'}),
'volume': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'files'", 'to': "orm['archive.Volume']"})
},
'archive.stream': {
'Meta': {'unique_together': "(('file', 'resolution', 'format'),)", 'object_name': 'Stream'},
'aspect_ratio': ('django.db.models.fields.FloatField', [], {'default': '0'}),
'available': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'color': ('ox.django.fields.TupleField', [], {'default': '[]'}),
'cuts': ('ox.django.fields.TupleField', [], {'default': '[]'}),
'duration': ('django.db.models.fields.FloatField', [], {'default': '0'}),
'error': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
'file': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'streams'", 'to': "orm['archive.File']"}),
'format': ('django.db.models.fields.CharField', [], {'default': "'webm'", 'max_length': '255'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'info': ('ox.django.fields.DictField', [], {'default': '{}'}),
'media': ('django.db.models.fields.files.FileField', [], {'default': 'None', 'max_length': '100', 'blank': 'True'}),
'oshash': ('django.db.models.fields.CharField', [], {'max_length': '16', 'null': 'True', 'db_index': 'True'}),
'resolution': ('django.db.models.fields.IntegerField', [], {'default': '96'}),
'source': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'related_name': "'derivatives'", 'null': 'True', 'to': "orm['archive.Stream']"}),
'volume': ('django.db.models.fields.FloatField', [], {'default': '0'})
},
'archive.volume': {
'Meta': {'unique_together': "(('user', 'name'),)", 'object_name': 'Volume'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '1024'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'volumes'", 'to': "orm['auth.User']"})
},
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'item.item': {
'Meta': {'object_name': 'Item'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'data': ('ox.django.fields.DictField', [], {'default': '{}'}),
'external_data': ('ox.django.fields.DictField', [], {'default': '{}'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'items'", 'blank': 'True', 'to': "orm['auth.Group']"}),
'icon': ('django.db.models.fields.files.ImageField', [], {'default': 'None', 'max_length': '100', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'itemId': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128', 'blank': 'True'}),
'json': ('ox.django.fields.DictField', [], {'default': '{}'}),
'level': ('django.db.models.fields.IntegerField', [], {'db_index': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'oxdbId': ('django.db.models.fields.CharField', [], {'max_length': '42', 'unique': 'True', 'null': 'True', 'blank': 'True'}),
'poster': ('django.db.models.fields.files.ImageField', [], {'default': 'None', 'max_length': '100', 'blank': 'True'}),
'poster_frame': ('django.db.models.fields.FloatField', [], {'default': '-1'}),
'poster_height': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'poster_source': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'poster_width': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'rendered': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
'stream_aspect': ('django.db.models.fields.FloatField', [], {'default': '1.3333333333333333'}),
'stream_info': ('ox.django.fields.DictField', [], {'default': '{}'}),
'torrent': ('django.db.models.fields.files.FileField', [], {'default': 'None', 'max_length': '1000', 'blank': 'True'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'items'", 'null': 'True', 'to': "orm['auth.User']"})
}
}
complete_apps = ['archive']

View file

@ -3,8 +3,9 @@
from __future__ import division, with_statement
import os.path
import time
import shutil
import tempfile
import time
from django.conf import settings
from django.contrib.auth.models import User
@ -13,6 +14,7 @@ from django.db.models.signals import pre_delete
from ox.django import fields
import ox
import ox.iso
from item import utils
import item.models
@ -48,7 +50,7 @@ class File(models.Model):
#editable
extension = models.CharField(default="", max_length=255, null=True)
language = models.CharField(default="", max_length=8, null=True)
language = models.CharField(default="", max_length=255, null=True)
part = models.CharField(default="", max_length=255, null=True)
part_title = models.CharField(default="", max_length=255, null=True)
version = models.CharField(default="", max_length=255, null=True)
@ -412,6 +414,60 @@ class File(models.Model):
import tasks
return tasks.process_stream.delay(self.id)
def extract_tracks(self):
'''
extract audio tracks from direct upload
'''
audio = self.info.get('audio', [])
if self.data and len(audio) > 1:
config = settings.CONFIG['video']
resolution = self.stream_resolution()
ffmpeg = ox.file.cmd('ffmpeg')
if ffmpeg == 'ffmpeg':
ffmpeg = None
tmp = tempfile.mkdtemp()
languages = [settings.CONFIG['language']]
for i, a in enumerate(audio[1:]):
media = self.data.path
info = ox.avinfo(media)
lang = ox.iso.langCode3To2(a.get('language', u'und').encode('utf-8'))
if not lang:
lang = settings.CONFIG['language']
language = lang
n = 2
while language in languages:
language = '%s%d' % (lang, n)
n += 1
profile = '%s.%s' % (resolution, config['formats'][0])
target = os.path.join(tmp, language + '_' + profile)
ok, error = extract.stream(media, target, profile, info, ffmpeg,
audio_track=i+1)
if ok:
tinfo = ox.avinfo(target)
del tinfo['path']
f = File(oshash=tinfo['oshash'], item=self.item)
f.path = self.path
f.info = tinfo
f.info['language'] = language
f.info['extension'] = config['formats'][0]
f.parse_info()
f.selected = True
f.save()
stream, created = Stream.objects.get_or_create(
file=f, resolution=resolution, format=config['formats'][0]
)
if created:
stream.media.name = stream.path(stream.name())
ox.makedirs(os.path.dirname(stream.media.path))
shutil.move(target, stream.media.path)
stream.available = True
stream.save()
stream.make_timeline()
stream.extract_derivatives()
if os.path.exists(target):
os.unlink(target)
shutil.rmtree(tmp)
def delete(self, *args, **kwargs):
self.delete_files()
super(File, self).delete(*args, **kwargs)

View file

@ -139,6 +139,7 @@ def extract_stream(fileId):
file.item.update_timeline()
if file.item.rendered:
file.item.save()
file.extract_tracks()
models.File.objects.filter(id=fileId).update(encoding=False)
@task(queue="encoding")

View file

@ -399,6 +399,7 @@ def editMedia(request):
data = json.loads(request.POST['data'])
ignore = []
save_items = []
dont_ignore = []
response = json_response(status=200, text='updated')
response['data']['files'] = []
@ -414,6 +415,8 @@ def editMedia(request):
for key in f.PATH_INFO:
if key in info:
f.info[key] = info[key]
if key == 'language' and (f.is_video or f.is_audio):
save_items.append(f.item)
update = True
if update:
f.save()
@ -430,6 +433,9 @@ def editMedia(request):
for i in Item.objects.filter(files__in=files).distinct():
i.update_selected()
i.update_wanted()
if save_items:
for i in Item.objects.filter(id__in=list(set(save_items))):
i.save()
return render_to_json_response(response)
actions.register(editMedia, cache=False)

View file

@ -604,6 +604,7 @@ class Item(models.Model):
streams = self.streams()
i['durations'] = [s.duration for s in streams]
i['duration'] = sum(i['durations'])
i['audioTracks'] = self.audio_tracks()
if not streams:
i['duration'] = self.files.filter(
Q(selected=True)|Q(wanted=True)
@ -1239,12 +1240,27 @@ class Item(models.Model):
self.torrent.name = torrent[len(settings.MEDIA_ROOT)+1:]
self.save()
def streams(self):
return archive.models.Stream.objects.filter(
def audio_tracks(self):
tracks = [f['language'] for f in self.files.filter(selected=True).values('language') if f['language']]
return sorted(set(tracks))
def streams(self, track=None):
qs = archive.models.Stream.objects.filter(
source=None, available=True, file__item=self, file__selected=True
).filter(
Q(file__is_audio=True)|Q(file__is_video=True)
).order_by('file__part', 'file__sort_path')
)
if not track:
tracks = self.audio_tracks()
if len(tracks) > 1:
if settings.CONFIG['language'] in tracks:
track = settings.CONFIG['language']
else:
track = tracks[0]
if track:
qs = qs.filter(file__language=track)
qs = qs.order_by('file__part', 'file__sort_path')
return qs
def update_timeline(self, force=False, async=True):
streams = self.streams()

View file

@ -19,6 +19,7 @@ urlpatterns = patterns("item.views",
#video
(r'^(?P<id>[A-Z0-9].*)/(?P<resolution>\d+)p(?P<index>\d*)\.(?P<format>webm|ogv|mp4)$', 'video'),
(r'^(?P<id>[A-Z0-9].*)/(?P<resolution>\d+)p(?P<index>\d*)\.(?P<track>.+)\.(?P<format>webm|ogv|mp4)$', 'video'),
#torrent
(r'^(?P<id>[A-Z0-9].*)/torrent$', 'torrent'),

View file

@ -897,7 +897,7 @@ def torrent(request, id, filename=None):
quote(os.path.basename(filename.encode('utf-8')))
return response
def video(request, id, resolution, format, index=None):
def video(request, id, resolution, format, index=None, track=None):
resolution = int(resolution)
resolutions = sorted(settings.CONFIG['video']['resolutions'])
if resolution not in resolutions:
@ -909,7 +909,7 @@ def video(request, id, resolution, format, index=None):
index = int(index) - 1
else:
index = 0
streams = item.streams()
streams = item.streams(track)
if index + 1 > streams.count():
raise Http404
stream = streams[index].get(resolution, format)