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