Merge remote-tracking branch 'brett/itsdangerous'
[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(proc_state):
46 """Code to process uploaded audio. Will be run by celery.
47
48 A Workbench() represents a local tempory dir. It is automatically
49 cleaned up when this function exits.
50 """
51 entry = proc_state.entry
52 workbench = proc_state.workbench
53 audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
54
55 queued_filepath = entry.queued_media_file
56 queued_filename = workbench.localized_file(
57 mgg.queue_store, queued_filepath,
58 'source')
59 name_builder = FilenameBuilder(queued_filename)
60
61 webm_audio_filepath = create_pub_filepath(
62 entry,
63 '{original}.webm'.format(
64 original=os.path.splitext(
65 queued_filepath[-1])[0]))
66
67 if audio_config['keep_original']:
68 with open(queued_filename, 'rb') as queued_file:
69 original_filepath = create_pub_filepath(
70 entry, name_builder.fill('{basename}{ext}'))
71
72 with mgg.public_store.get_file(original_filepath, 'wb') as \
73 original_file:
74 _log.debug('Saving original...')
75 original_file.write(queued_file.read())
76
77 entry.media_files['original'] = original_filepath
78
79 transcoder = AudioTranscoder()
80
81 with NamedTemporaryFile(dir=workbench.dir) as webm_audio_tmp:
82 progress_callback = ProgressCallback(entry)
83
84 transcoder.transcode(
85 queued_filename,
86 webm_audio_tmp.name,
87 quality=audio_config['quality'],
88 progress_callback=progress_callback)
89
90 transcoder.discover(webm_audio_tmp.name)
91
92 _log.debug('Saving medium...')
93 mgg.public_store.get_file(webm_audio_filepath, 'wb').write(
94 webm_audio_tmp.read())
95
96 entry.media_files['webm_audio'] = webm_audio_filepath
97
98 # entry.media_data_init(length=int(data.audiolength))
99
100 if audio_config['create_spectrogram']:
101 spectrogram_filepath = create_pub_filepath(
102 entry,
103 '{original}-spectrogram.jpg'.format(
104 original=os.path.splitext(
105 queued_filepath[-1])[0]))
106
107 with NamedTemporaryFile(dir=workbench.dir, suffix='.ogg') as wav_tmp:
108 _log.info('Creating OGG source for spectrogram')
109 transcoder.transcode(
110 queued_filename,
111 wav_tmp.name,
112 mux_string='vorbisenc quality={0} ! oggmux'.format(
113 audio_config['quality']))
114
115 thumbnailer = AudioThumbnailer()
116
117 with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as spectrogram_tmp:
118 thumbnailer.spectrogram(
119 wav_tmp.name,
120 spectrogram_tmp.name,
121 width=mgg.global_config['media:medium']['max_width'],
122 fft_size=audio_config['spectrogram_fft_size'])
123
124 _log.debug('Saving spectrogram...')
125 mgg.public_store.get_file(spectrogram_filepath, 'wb').write(
126 spectrogram_tmp.read())
127
128 entry.media_files['spectrogram'] = spectrogram_filepath
129
130 with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as thumb_tmp:
131 thumbnailer.thumbnail_spectrogram(
132 spectrogram_tmp.name,
133 thumb_tmp.name,
134 (mgg.global_config['media:thumb']['max_width'],
135 mgg.global_config['media:thumb']['max_height']))
136
137 thumb_filepath = create_pub_filepath(
138 entry,
139 '{original}-thumbnail.jpg'.format(
140 original=os.path.splitext(
141 queued_filepath[-1])[0]))
142
143 mgg.public_store.get_file(thumb_filepath, 'wb').write(
144 thumb_tmp.read())
145
146 entry.media_files['thumb'] = thumb_filepath
147 else:
148 entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg']
149
150 # Remove queued media file from storage and database.
151 # queued_filepath is in the task_id directory which should
152 # be removed too, but fail if the directory is not empty to be on
153 # the super-safe side.
154 mgg.queue_store.delete_file(queued_filepath) # rm file
155 mgg.queue_store.delete_dir(queued_filepath[:-1]) # rm dir
156 entry.queued_media_file = []