62daf412309aa872d20028c472285368d5f1a3ae
[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, BadMediaFail
23
24 from mediagoblin.media_types.audio.transcoders import AudioTranscoder, \
25 AudioThumbnailer
26
27 _log = logging.getLogger(__name__)
28
29 def sniff_handler(media_file, **kw):
30 try:
31 transcoder = AudioTranscoder()
32 data = transcoder.discover(media_file.name)
33 except BadMediaFail:
34 _log.debug('Audio discovery raised BadMediaFail')
35 return False
36
37 if data.is_audio == True and data.is_video == False:
38 return True
39
40 return False
41
42 def process_audio(entry):
43 audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
44
45 workbench = mgg.workbench_manager.create_workbench()
46
47 queued_filepath = entry.queued_media_file
48 queued_filename = workbench.localized_file(
49 mgg.queue_store, queued_filepath,
50 'source')
51
52 ogg_filepath = create_pub_filepath(
53 entry,
54 '{original}.webm'.format(
55 original=os.path.splitext(
56 queued_filepath[-1])[0]))
57
58 transcoder = AudioTranscoder()
59
60 with tempfile.NamedTemporaryFile() as ogg_tmp:
61
62 transcoder.transcode(
63 queued_filename,
64 ogg_tmp.name,
65 quality=audio_config['quality'])
66
67 data = transcoder.discover(ogg_tmp.name)
68
69 _log.debug('Saving medium...')
70 mgg.public_store.get_file(ogg_filepath, 'wb').write(
71 ogg_tmp.read())
72
73 entry.media_files['ogg'] = ogg_filepath
74
75 entry.media_data['audio'] = {
76 u'length': int(data.audiolength)}
77
78 if audio_config['create_spectrogram']:
79 spectrogram_filepath = create_pub_filepath(
80 entry,
81 '{original}-spectrogram.jpg'.format(
82 original=os.path.splitext(
83 queued_filepath[-1])[0]))
84
85 with tempfile.NamedTemporaryFile(suffix='.wav') as wav_tmp:
86 _log.info('Creating WAV source for spectrogram')
87 transcoder.transcode(
88 queued_filename,
89 wav_tmp.name,
90 mux_string='wavenc')
91
92 thumbnailer = AudioThumbnailer()
93
94 with tempfile.NamedTemporaryFile(suffix='.jpg') as spectrogram_tmp:
95 thumbnailer.spectrogram(
96 wav_tmp.name,
97 spectrogram_tmp.name,
98 width=mgg.global_config['media:medium']['max_width'])
99
100 _log.debug('Saving spectrogram...')
101 mgg.public_store.get_file(spectrogram_filepath, 'wb').write(
102 spectrogram_tmp.read())
103
104 entry.media_files['spectrogram'] = spectrogram_filepath
105
106 with tempfile.NamedTemporaryFile(suffix='.jpg') as thumb_tmp:
107 thumbnailer.thumbnail_spectrogram(
108 spectrogram_tmp.name,
109 thumb_tmp.name,
110 (mgg.global_config['media:thumb']['max_width'],
111 mgg.global_config['media:thumb']['max_height']))
112
113 thumb_filepath = create_pub_filepath(
114 entry,
115 '{original}-thumbnail.jpg'.format(
116 original=os.path.splitext(
117 queued_filepath[-1])[0]))
118
119 mgg.public_store.get_file(thumb_filepath, 'wb').write(
120 thumb_tmp.read())
121
122 entry.media_files['thumb'] = thumb_filepath
123 else:
124 entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg']
125
126 mgg.queue_store.delete_file(queued_filepath)
127
128 entry.save()
129
130 # clean up workbench
131 workbench.destroy_self()