Audio media handler, media sniffing, video fixes
[mediagoblin.git] / mediagoblin / media_types / audio / processing.py
CommitLineData
5a34a80d
JW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import logging
18import tempfile
19import os
20
21from mediagoblin import mg_globals as mgg
22from mediagoblin.processing import create_pub_filepath
23
24from mediagoblin.media_types.audio.transcoders import AudioTranscoder
25
26_log = logging.getLogger()
27
28def sniff_handler(media):
29 return True
30
31def process_audio(entry):
32 audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
33
34 workbench = mgg.workbench_manager.create_workbench()
35
36 queued_filepath = entry.queued_media_file
37 queued_filename = workbench.localized_file(
38 mgg.queue_store, queued_filepath,
39 'source')
40
41 ogg_filepath = create_pub_filepath(
42 entry,
43 '{original}.webm'.format(
44 original=os.path.splitext(
45 queued_filepath[-1])[0]))
46
47 ogg_tmp = tempfile.NamedTemporaryFile()
48
49 with ogg_tmp:
50 transcoder = AudioTranscoder()
51
52 transcoder.transcode(
53 queued_filename,
54 ogg_tmp.name,
55 quality=audio_config['quality'])
56
57 data = transcoder.discover(ogg_tmp.name)
58
59 _log.debug('Saving medium...')
60 mgg.public_store.get_file(ogg_filepath, 'wb').write(
61 ogg_tmp.read())
62
63 entry.media_files['ogg'] = ogg_filepath
64
65 entry.media_data['audio'] = {
66 u'length': int(data.audiolength)}
67
68 thumbnail_tmp = tempfile.NamedTemporaryFile()
69
70 with thumbnail_tmp:
71 entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg']
72
73 mgg.queue_store.delete_file(queued_filepath)
74
75 entry.save()