CELERY_ALWAYS_EAGER environment variable only recognized if 'true' now
[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
4f4f2531 22from mediagoblin.processing import create_pub_filepath, BadMediaFail
5a34a80d 23
10085b77
JW
24from mediagoblin.media_types.audio.transcoders import AudioTranscoder, \
25 AudioThumbnailer
5a34a80d 26
10085b77 27_log = logging.getLogger(__name__)
5a34a80d 28
ec4261a4 29def sniff_handler(media_file, **kw):
4f4f2531
JW
30 try:
31 transcoder = AudioTranscoder()
ec4261a4 32 data = transcoder.discover(media_file.name)
4f4f2531
JW
33 except BadMediaFail:
34 _log.debug('Audio discovery raised BadMediaFail')
35 return False
ec4261a4 36
4f4f2531
JW
37 if data.is_audio == True and data.is_video == False:
38 return True
10085b77
JW
39
40 return False
5a34a80d
JW
41
42def 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
10085b77 58 transcoder = AudioTranscoder()
5a34a80d 59
10085b77 60 with tempfile.NamedTemporaryFile() as ogg_tmp:
5a34a80d
JW
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
c7cf6235 75 # entry.media_data_init(length=int(data.audiolength))
5a34a80d 76
10085b77
JW
77 if audio_config['create_spectrogram']:
78 spectrogram_filepath = create_pub_filepath(
79 entry,
80 '{original}-spectrogram.jpg'.format(
81 original=os.path.splitext(
82 queued_filepath[-1])[0]))
83
84 with tempfile.NamedTemporaryFile(suffix='.wav') as wav_tmp:
85 _log.info('Creating WAV source for spectrogram')
86 transcoder.transcode(
87 queued_filename,
88 wav_tmp.name,
89 mux_string='wavenc')
90
91 thumbnailer = AudioThumbnailer()
92
93 with tempfile.NamedTemporaryFile(suffix='.jpg') as spectrogram_tmp:
94 thumbnailer.spectrogram(
95 wav_tmp.name,
96 spectrogram_tmp.name,
97 width=mgg.global_config['media:medium']['max_width'])
98
99 _log.debug('Saving spectrogram...')
100 mgg.public_store.get_file(spectrogram_filepath, 'wb').write(
101 spectrogram_tmp.read())
102
103 entry.media_files['spectrogram'] = spectrogram_filepath
104
105 with tempfile.NamedTemporaryFile(suffix='.jpg') as thumb_tmp:
106 thumbnailer.thumbnail_spectrogram(
107 spectrogram_tmp.name,
108 thumb_tmp.name,
109 (mgg.global_config['media:thumb']['max_width'],
110 mgg.global_config['media:thumb']['max_height']))
111
112 thumb_filepath = create_pub_filepath(
113 entry,
114 '{original}-thumbnail.jpg'.format(
115 original=os.path.splitext(
116 queued_filepath[-1])[0]))
117
118 mgg.public_store.get_file(thumb_filepath, 'wb').write(
119 thumb_tmp.read())
120
121 entry.media_files['thumb'] = thumb_filepath
122 else:
5a34a80d 123 entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg']
10085b77 124
5a34a80d
JW
125 mgg.queue_store.delete_file(queued_filepath)
126
127 entry.save()
e2007352
JW
128
129 # clean up workbench
130 workbench.destroy_self()