<commit_before># -*- coding: utf8 -*-

from __future__ import absolute_import
from __future__ import unicode_literals

import os
from celery import shared_task, chain

from django.conf import settings

from .models import Video


@shared_task(
        name="notaro.create_poster",
        queue="video",
        ignore_result=True)
def create_poster(video_id):
    # pylint: disable=no-member
    v = Video.objects.get(id=video_id)
    out = os.path.join(
        settings.FILEBROWSER_DIRECTORY, 'videos', v.directory, 'poster.jpg')
    os.system(
            ('ffmpeg -i {infile} -ss 3 -f image2 -vframes 1 ' +
             '-y {out}').format(
                 infile=v.video.path_full,
                 out=os.path.join(settings.MEDIA_ROOT, out)))
    v.poster = out
    v.save()
    print 'saved poster', v.poster


@shared_task(
        name="notaro.create_video_version",
        queue="video",
        ignore_result=True)
def create_video_version(video_id, fmt):
    # pylint: disable=no-member
    v = Video.objects.get(id=video_id)
    fn = v.video.path_full
    target = os.path.join(
            settings.MEDIA_ROOT, settings.FILEBROWSER_DIRECTORY,
            'videos', v.directory, 'video')

    options = ''
    if fmt == 'ogv':
        options = '-codec:v libtheora -qscale:v 7 ' +\
                  '-codec:a libvorbis -qscale:a 5'
    elif fmt == 'webm':
        options = '-c:v libvpx -crf 8 -b:v 1M -c:a libvorbis'
    elif fmt == 'mp4':
        options = '-c:v libx264 -profile:v baseline -level 3.0 ' +\
                  '-preset slow -crf 22 -c:a copy'

    os.system('ffmpeg -i {fn} {options} -y {tgt}.{fmt}'.format(
        fn=fn, options=options, tgt=target, fmt=fmt))


@shared_task(name="notaro.compile_video", queue="video")
def compile_video(video_id):
    s0 = create_poster.si(video_id).set(queue='video')
    s1 = create_video_version.si(video_id, fmt='mp4').set(queue='video')<commit_msg>Experiment further with ffmpeg options.<commit_after># -*- coding: utf8 -*-

from __future__ import absolute_import
from __future__ import unicode_literals

import os
from celery import shared_task, chain

from django.conf import settings

from .models import Video


@shared_task(
        name="notaro.create_poster",
        queue="video",
        ignore_result=True)
def create_poster(video_id):
    # pylint: disable=no-member
    v = Video.objects.get(id=video_id)
    out = os.path.join(
        settings.FILEBROWSER_DIRECTORY, 'videos', v.directory, 'poster.jpg')
    os.system(
            ('ffmpeg -i {infile} -ss 3 -f image2 -vframes 1 ' +
             '-y {out}').format(
                 infile=v.video.path_full,
                 out=os.path.join(settings.MEDIA_ROOT, out)))
    v.poster = out
    v.save()
    print 'saved poster', v.poster


@shared_task(
        name="notaro.create_video_version",
        queue="video",
        ignore_result=True)
def create_video_version(video_id, fmt):
    # pylint: disable=no-member
    v = Video.objects.get(id=video_id)
    fn = v.video.path_full
    target = os.path.join(
            settings.MEDIA_ROOT, settings.FILEBROWSER_DIRECTORY,
            'videos', v.directory, 'video')

    options = ''
    if fmt == 'ogv':
        options = '-codec:v libtheora -qscale:v 7 ' +\
                  '-codec:a libvorbis -qscale:a 5'
    elif fmt == 'webm':
        options = '-c:v libvpx -crf 8 -b:v 1M -c:a libvorbis'
    elif fmt == 'mp4':
        options = '-c:v libx265 -profile:v baseline -level 3.0 ' +\
                  '-preset slow -crf 22 -c:a libfdk_aac -b:a 256k'

    os.system('ffmpeg -i {fn} {options} -y {tgt}.{fmt}'.format(
        fn=fn, options=options, tgt=target, fmt=fmt))


@shared_task(name="notaro.compile_video", queue="video")
def compile_video(video_id):
    s0 = create_poster.si(video_id).set(queue='video')
    s1 = create_video_version.si(video_id, fmt='mp4').set(queue='video')
{'fraction_non_alphanumeric': 0.138188368502316, 'fraction_numerical': 0.010550694801852806, 'mean_word_length': 2.6497652582159623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1251846', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1340, 'n_words': 293}