beb12391885c718d805223a649bdd91587d4c75f
[mediagoblin.git] / mediagoblin / media_types / audio / processing.py
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
17 import logging
18 import tempfile
19 import os
20
21 from mediagoblin import mg_globals as mgg
22 from mediagoblin.processing import create_pub_filepath
23
24 from mediagoblin.media_types.audio.transcoders import AudioTranscoder
25
26 _log = logging.getLogger()
27
28 def sniff_handler(media):
29 return True
30
31 def 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()