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