Audio thumbnailing & spectrograms, media plugins use sniffing
[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
22from mediagoblin.processing import create_pub_filepath
23
10085b77
JW
24from mediagoblin.media_types.audio.transcoders import AudioTranscoder, \
25 AudioThumbnailer
5a34a80d 26
10085b77 27_log = logging.getLogger(__name__)
5a34a80d 28
ec4261a4
JW
29def sniff_handler(media_file, **kw):
30 transcoder = AudioTranscoder()
31 try:
32 data = transcoder.discover(media_file.name)
33
34 if data.is_audio == True and data.is_video == False:
35 return True
36 except:
10085b77
JW
37 pass
38
39 return False
5a34a80d
JW
40
41def process_audio(entry):
42 audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
43
44 workbench = mgg.workbench_manager.create_workbench()
45
46 queued_filepath = entry.queued_media_file
47 queued_filename = workbench.localized_file(
48 mgg.queue_store, queued_filepath,
49 'source')
50
51 ogg_filepath = create_pub_filepath(
52 entry,
53 '{original}.webm'.format(
54 original=os.path.splitext(
55 queued_filepath[-1])[0]))
56
10085b77 57 transcoder = AudioTranscoder()
5a34a80d 58
10085b77 59 with tempfile.NamedTemporaryFile() as ogg_tmp:
5a34a80d
JW
60
61 transcoder.transcode(
62 queued_filename,
63 ogg_tmp.name,
64 quality=audio_config['quality'])
65
66 data = transcoder.discover(ogg_tmp.name)
67
68 _log.debug('Saving medium...')
69 mgg.public_store.get_file(ogg_filepath, 'wb').write(
70 ogg_tmp.read())
71
72 entry.media_files['ogg'] = ogg_filepath
73
74 entry.media_data['audio'] = {
75 u'length': int(data.audiolength)}
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()