BitMover/_audio.py

77 lines
2.9 KiB
Python

#!/usr/bin/env python
import os.path
import ffmpeg
import time
from datetime import datetime
from _media_file import MediaFile
class AudioFile(MediaFile):
def __init__(self,*args,**kwargs):
super(AudioFile, self).__init__(*args, **kwargs)
self.args = args
self.kwargs = kwargs
self.file = kwargs['file']
self.probe = ffmpeg.probe(self.path_file_name)
self.audio_capture_date = self.get_audio_capture_date()
if 'video' == self.probe['streams'][0]['codec_type'].lower():
self.video_stream = self.probe['streams'][0]
elif 'video' == self.probe['streams'][1]['codec_type'].lower():
self.video_stream = self.probe['streams'][1]
elif 'video' == self.probe['streams'][2]['codec_type'].lower():
self.video_stream = self.probe['streams'][2]
if 'audio' == self.probe['streams'][0]['codec_type'].lower():
self.audio_stream = self.probe['streams'][0]
elif 'audio' == self.probe['streams'][1]['codec_type'].lower():
self.audio_stream = self.probe['streams'][1]
elif 'audio' == self.probe['streams'][2]['codec_type'].lower():
self.audio_stream = self.probe['streams'][2]
self.format_stream = self.probe['format']
self.stream = {}
@staticmethod
def convert_from_seconds(seconds):
return time.strftime("%H:%M:%S", time.gmtime(seconds))
def get_audio_capture_date(self):
#TODO: refactor this try/except logic.
try:
stamp = datetime.strptime(
self.format_stream['tags']['date'],
'%Y-%m-%d'
)
except KeyError:
try:
stamp = datetime.fromtimestamp(os.path.getctime(self.path_file_name))
except:
stamp = datetime.strptime(
str('1900:01:01 00:00:00'),
'%Y:%m:%d %H:%M:%S'
)
return stamp
def get_video_meta(self):
self.stream = {
'video': {
'bits_per_raw_sample': self.video_stream['bits_per_raw_sample'],
'codec_long_name': self.video_stream['codec_long_name'],
'duration': self.convert_from_seconds(float(self.video_stream['duration'])),
'encoding_brand': self.format_stream['tags']['major_brand'],
'pix_fmt': self.video_stream['pix_fmt'],
'profile': self.video_stream['profile'],
'r_frame_rate': self.video_stream['r_frame_rate'],
'size': {
'width_height': self.size,
'height': self.video_stream['height'],
'width': self.video_stream['width']
}
},
'audio': {},
'format': {}
}
return self.stream