<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Ubuntu Podcast
# http://www.ubuntupodcast.org
# See the file "LICENSE" for the full license governing this code.

import base64
import optparse
import sys
from collections import OrderedDict
from ffmpy import FF
from mutagen.easyid3 import EasyID3, EasyID3KeyError
from mutagen.flac import Picture
from mutagen.mp3 import MP3
from mutagen.oggvorbis import OggVorbis
from mutagen.id3 import APIC, COMM, ID3, error
from pydub import AudioSegment
from PIL import Image, ImageDraw, ImageFont

def audio_encode(config, audio_format):
    if audio_format is 'mp3':
        audio_file = config.mp3_file
        audio_bitrate = config.mp3['bitrate']
        audio_channels = config.mp3['channels']
        audio_codec = config.mp3['codec']
    elif audio_format is 'ogg':
        audio_file = config.ogg_file
        audio_bitrate = config.ogg['bitrate']
        audio_channels = config.ogg['channels']
        audio_codec = config.ogg['codec']
    else:
        print("ERROR! Unkown audio format requested. Abort.")
        sys.exit(1)

    print("Encoding " + audio_file)
    AudioSegment.from_file(config.audio_in).export(audio_file,
        bitrate=audio_bitrate,
        codec=audio_codec,
        format=audio_format,
        parameters=['-ac', audio_channels, '-threads', '0']
        )

def mp3_tag(config):
    print("Tagging " + config.mp3_file)
    # Make sure the MP3 has tag headers.
    try:
        audio = EasyID3(config.mp3_file)
    except mutagen.id3.error:
        audio = EasyID3()
        audio.save(config.mp3_file)

    # Add the tags.
    for k in config.tags.keys():
        if k == 'comments':
            continue
        else:
            try:
                audio[k] = config.tags[k]
            except EasyID3KeyError:
                print("%s is an invalid ID3 key" % k)
    audio.save(config.mp3_file)

    # Comments are not supported via EasyID3
    audio = ID3(config.mp3_file)
    audio["COMM"] = COMM(encoding=3, lang=config.tags['language'], desc='desc', text=config.tags['comments'])
    audio.save(config.mp3_file)

def mp3_coverart(config):
    print("Adding cover art to " + config.mp3_file)
    imgdata = open(config.coverart,'rb').read()

    audio=MP3(config.mp3_file, ID3=ID3);
    # FIXME: Derive the image mime type.
    audio.tags.add(APIC(encoding=3,
                        mime='image/png',
                        type=3,
                        desc=u'Cover',
                        data=imgdata))
    audio.save()


def ogg_tag(config):
    print("Tagging " + config.ogg_file)
    audio = OggVorbis(config.ogg_file)

    # Add the tags.
    for k in config.tags.keys():
        audio[k] = config.tags[k]
    audio.save()

def ogg_coverart(config):
    print("Adding cover art to " + config.ogg_file)
    coverart = config.coverart
    imgdata = open(coverart,'rb').read()

    im = Image.open(coverart)
    w,h = im.size

    p = Picture()
    p.data = imgdata
    p.type = 3
    p.desc = u'Cover'
    # FIXME: Derive the image mime type.
    p.mime = 'image/png';
    p.width = w
    p.height = h
    p.depth = 24
    dt=p.write();
    enc=base64.b64encode(dt).decode('ascii');

    audio = OggVorbis(config.ogg_file)
    audio['metadata_block_picture']=[enc];
    audio.save()

def img_resize(img_file, width, height):
    #response = requests.get(headerimageurl)
    img = Image.open(img_file)

    # Check the image size.
    if img.size[0] < width or img.size[1] < height:
        print("Sorry, the image needs to larger than the requested resize. Abort.")
        sys.exit(1)

    scale_x_down_by = img.size[0] // width
    scale_y_down_by = img.size[1] // height

    if scale_x_down_by > scale_y_down_by:
        resize_to = (int(img.size[0] // scale_y_down_by), height)
        x_border = int(resize_to[0]) - width
        crop = (x_border // 2, 0, width + (x_border // 2), height)
    else:
        resize_to = (width, int(img.size[1] // scale_x_down_by))
        y_border = int(resize_to[1]) - height
        crop = (0, y_border // 2, width, height + (y_border // 2))
    img.thumbnail(resize_to, Image.ANTIALIAS)
    cropped = img.crop(crop)
    return cropped

def png_header(config):
    print("Creating " + config.png_header_file)
    header = img_resize(config.backdrop, config.img_header_width, config.img_header_height)
    header.save(config.png_header_file)
<commit_msg>Remove obsolete imports and use correct mime type.<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Ubuntu Podcast
# http://www.ubuntupodcast.org
# See the file "LICENSE" for the full license governing this code.

import base64
import sys
from collections import OrderedDict
from ffmpy import FF
from mutagen.easyid3 import EasyID3, EasyID3KeyError
from mutagen.flac import Picture
from mutagen.mp3 import MP3
from mutagen.oggvorbis import OggVorbis
from mutagen.id3 import APIC, COMM, ID3, error
from pydub import AudioSegment
from PIL import Image, ImageDraw, ImageFont

def audio_encode(config, audio_format):
    if audio_format is 'mp3':
        audio_file = config.mp3_file
        audio_bitrate = config.mp3['bitrate']
        audio_channels = config.mp3['channels']
        audio_codec = config.mp3['codec']
    elif audio_format is 'ogg':
        audio_file = config.ogg_file
        audio_bitrate = config.ogg['bitrate']
        audio_channels = config.ogg['channels']
        audio_codec = config.ogg['codec']
    else:
        print("ERROR! Unkown audio format requested. Abort.")
        sys.exit(1)

    print("Encoding " + audio_file)
    AudioSegment.from_file(config.audio_in).export(audio_file,
        bitrate=audio_bitrate,
        codec=audio_codec,
        format=audio_format,
        parameters=['-ac', audio_channels, '-threads', '0']
        )

def mp3_tag(config):
    print("Tagging " + config.mp3_file)
    # Make sure the MP3 has tag headers.
    try:
        audio = EasyID3(config.mp3_file)
    except mutagen.id3.error:
        audio = EasyID3()
        audio.save(config.mp3_file)

    # Add the tags.
    for k in config.tags.keys():
        if k == 'comments':
            continue
        else:
            try:
                audio[k] = config.tags[k]
            except EasyID3KeyError:
                print("%s is an invalid ID3 key" % k)
    audio.save(config.mp3_file)

    # Comments are not supported via EasyID3
    audio = ID3(config.mp3_file)
    audio["COMM"] = COMM(encoding=3, lang=config.tags['language'], desc='desc', text=config.tags['comments'])
    audio.save(config.mp3_file)

def mp3_coverart(config):
    print("Adding cover art to " + config.mp3_file)
    imgdata = open(config.coverart,'rb').read()

    audio=MP3(config.mp3_file, ID3=ID3);
    audio.tags.add(APIC(encoding=3,
                        mime=config.covert_mime,
                        type=3,
                        desc='Cover',
                        data=imgdata))
    audio.save()

def ogg_tag(config):
    print("Tagging " + config.ogg_file)
    audio = OggVorbis(config.ogg_file)

    # Add the tags.
    for k in config.tags.keys():
        audio[k] = config.tags[k]
    audio.save()

def ogg_coverart(config):
    print("Adding cover art to " + config.ogg_file)
    coverart = config.coverart
    imgdata = open(coverart,'rb').read()

    im = Image.open(coverart)
    w,h = im.size

    p = Picture()
    p.data = imgdata
    p.type = 3
    p.desc = 'Cover'
    p.mime = config.coverart_mime
    p.width = w
    p.height = h
    p.depth = 24
    dt=p.write()
    enc=base64.b64encode(dt).decode('ascii')

    audio = OggVorbis(config.ogg_file)
    audio['metadata_block_picture']=[enc]
    audio.save()

def img_resize(img_file, width, height):
    img = Image.open(img_file)

    # Check the image size.
    if img.size[0] < width or img.size[1] < height:
        print("Sorry, the image needs to larger than the requested resize. Abort.")
        sys.exit(1)

    scale_x_down_by = img.size[0] // width
    scale_y_down_by = img.size[1] // height

    if scale_x_down_by > scale_y_down_by:
        resize_to = (int(img.size[0] // scale_y_down_by), height)
        x_border = int(resize_to[0]) - width
        crop = (x_border // 2, 0, width + (x_border // 2), height)
    else:
        resize_to = (width, int(img.size[1] // scale_x_down_by))
        y_border = int(resize_to[1]) - height
        crop = (0, y_border // 2, width, height + (y_border // 2))
    img.thumbnail(resize_to, Image.ANTIALIAS)
    cropped = img.crop(crop)
    return cropped

def png_header(config):
    print("Creating " + config.png_header_file)
    header = img_resize(config.backdrop, config.img_header_width, config.img_header_height)
    header.save(config.png_header_file)

{'fraction_non_alphanumeric': 0.11943713533920604, 'fraction_numerical': 0.014643633451550167, 'mean_word_length': 2.6700251889168767, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4784815', 'n_tokens_mistral': 3009, 'n_tokens_neox': 2896, 'n_words': 667}