~charclo-michael/charcloconverter/test

« back to all changes in this revision

Viewing changes to src/video_file.py

  • Committer: Michael Charclo
  • Date: 2008-05-13 22:10:56 UTC
  • Revision ID: charclo.michael@gmail.com-20080513221056-4uzmb7kpurfaujim
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Michael Charclo
 
2
 
3
# This program is free software; you can redistribute it and/or
 
4
# modify it under the terms of the GNU General Public License
 
5
# as published by the Free Software Foundation; either version 2
 
6
# of the License, or (at your option) any later version.
 
7
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
16
 
 
17
import os
 
18
import re
 
19
    
 
20
class VideoFile:
 
21
    """This is the videofile class"""
 
22
    
 
23
    __LOGFILE = os.path.expanduser("~/.encoder/logfile")
 
24
    
 
25
    def __init__(self, filename):
 
26
        self.filename = filename
 
27
        self.hours = 0
 
28
        self.minutes = 0
 
29
        self.seconds = 0
 
30
        self.audio_codec = 'N/A'
 
31
        self.fps = 'N/A'
 
32
        self.height = 'N/A'
 
33
        
 
34
    def get_metadata(self):
 
35
        """
 
36
        Get the metadata from a Video File
 
37
        
 
38
        To get the metadata from the Video File we let ffmpeg try to convert it
 
39
        to a non-existant location. The resulting info is scanned for metadata.        
 
40
        """
 
41
        
 
42
        #FIXME: This method could probably be done better with pymedia... or not?
 
43
        pipe = os.popen('''ffmpeg -i "%s" /dev/null &> %s''' %(self.filename, self.__LOGFILE))
 
44
        pipe.close()
 
45
        f = file(self.__LOGFILE)
 
46
        logfile = f.read()
 
47
 
 
48
        regexp = r'''Duration:\s(?P<hours>\d{2}):(?P<minutes>\d{2}):(?P<seconds>\d{2}.\d),'''
 
49
        match = re.search(regexp, logfile)
 
50
        self.hours = int(match.group('hours'))
 
51
        self.minutes = int(match.group('minutes'))
 
52
        self.seconds = float(match.group('seconds'))
 
53
        self.duration = '%d:%d:%.f' %(self.hours, self.minutes, self.seconds)
 
54
        
 
55
        regexp = r'''bitrate:\s(?P<video_bitrate>.*?)\s'''
 
56
        match = re.search(regexp, logfile)
 
57
        self.video_bitrate = match.group('video_bitrate')
 
58
        
 
59
        regexp = r'''Video:\s(?P<video_codec>.*?),'''
 
60
        match = re.search(regexp, logfile)
 
61
        self.video_codec = match.group('video_codec')
 
62
        
 
63
        regexp = r'''\s(?P<width>\d*?)x(?P<height>\d*?),'''
 
64
        match = re.search(regexp, logfile)
 
65
        self.width = match.group('width')
 
66
        self.height = match.group('height')
 
67
        self.size =  self.width + 'x' + self.height
 
68
        
 
69
##        self.aspect = str(float(match.group('width'))/float(match.group('height')))[:6]
 
70
 
 
71
        
 
72
        regexp = r'''\s(?P<fps>\d{1,2}\.\d{1,2})\sfps'''
 
73
        match = re.search(regexp, logfile)
 
74
        self.fps = float(match.group('fps'))
 
75
        
 
76
        regexp = r'''Audio:\s(?P<audio_codec>.*?),'''
 
77
        match = re.search(regexp, logfile)
 
78
        self.audio_codec = match.group('audio_codec')
 
79
        
 
80
        regexp = r'''Hz,\s(?P<mono_stereo>.*?),'''
 
81
        match = re.search(regexp, logfile)
 
82
        self.mono_stereo = match.group('mono_stereo')
 
83
        
 
84
        regexp = r''',\s(?P<audio_bitrate>\d{2,3})\skb/s'''
 
85
        match = re.search(regexp, logfile)
 
86
        self.audio_bitrate = match.group('audio_bitrate')
 
87
          
 
88
        f.close() 
 
89
        
 
90
        self.metadata = [self.duration, self.video_bitrate\
 
91
                , self.video_codec, self.size, self.fps, self.audio_codec\
 
92
                , self.mono_stereo, self.audio_bitrate]
 
93
        return self.metadata
 
94
    
 
95
    def get_filename(self):
 
96
        return self.filename
 
97
        
 
98
##    def get_hours(self):
 
99
##        return self.hours
 
100
##    
 
101
##    def get_minutes(self):
 
102
##        return self.minutes
 
103
##    
 
104
##    def get_seconds(self):
 
105
##        return self.seconds
 
106
##    
 
 
b'\\ No newline at end of file'