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