iamsultan avatar iamsultan committed 3de5a0c

* Initial commit

Comments (0)

Files changed (3)

Empty file added.

+# -*- coding: utf-8 -*-
+class KeyFrame:
+    def __init__(self, time=None, file_position=None):
+        self.time = time
+        self.file_position = file_position
+
+    def all_from_xml(self, metadata):
+        times = self.get(soup=metadata, tag="times", func=float)
+        file_positions = self.get(soup=metadata, tag="filepositions", func=int)
+        keyframes = []
+
+        for cnt in range(len(times)):
+            keyframes.append(KeyFrame(time=times[cnt], file_position=file_positions[cnt]))
+
+        return keyframes
+
+    def get(self, soup, tag, func):
+        self.metadata = soup
+        results = []
+
+        for time in self.value(tag=tag):
+            for val in time.findAll("value"):
+                results.append(func("".join(val.contents)))
+
+        return results
+
+    def value(self, tag=None, first=False, soup=None):
+        if tag is None:
+            raise NameError("Please provide tag should not be None")
+        if soup:
+            target = soup.first(tag)
+        else:
+            target = self.metadata.first(tag)
+        return "".join(target.contents) if first else self.metadata(tag)
+# -*- coding: utf-8 -*-
+import os, tempfile
+
+from BeautifulSoup import BeautifulStoneSoup
+from frames import KeyFrame
+
+class Yamdi:
+    def __init__(self, flv_path):
+        self.flv_path = flv_path
+        self.metadata = BeautifulStoneSoup(self.extract_meta(flv_path))
+
+    @property
+    def duration(self):
+        return float(self.value(tag="duration", first=True))
+
+    @property
+    def width(self):
+        return int(self.value(tag="width", first=True))
+
+    @property
+    def height(self):
+        return int(self.value(tag="height", first=True))
+
+    @property
+    def has_keyframes(self):
+        return self.boolean(self.value(tag="hasKeyframes", first=True))
+
+    @property
+    def has_video(self):
+        return self.boolean(self.value(tag="hasVideo", first=True))
+
+    @property
+    def has_audio(self):
+        return self.boolean(self.value(tag="hasAudio", first=True))
+
+    @property
+    def has_metadata(self):
+        return self.boolean(self.value(tag="hasMetadata", first=True))
+
+    @property
+    def has_cue_points(self):
+        return self.boolean(self.value(tag="hasCuePoint", first=True))
+
+    @property
+    def can_seek_to_end(self):
+        return self.boolean(self.value(tag="canSeekToEnd", first=True))
+
+    @property
+    def audio_codec_id(self):
+        return int(self.value(tag="audiocodecid", first=True))
+
+    @property
+    def audio_sample_rate(self):
+        return int(self.value(tag="audiosamplerate", first=True))
+
+    @property
+    def audio_data_rate(self):
+        return int(self.value(tag="audiodatarate", first=True))
+
+    @property
+    def audio_sample_size(self):
+        return int(self.value(tag="audiosamplesize", first=True))
+
+    @property
+    def audio_delay(self):
+        return float(self.value(tag="audiodelay", first=True))
+
+    @property
+    def stereo(self):
+        return self.boolean(text=self.value(tag="stereo", first=True))
+
+    @property
+    def video_codec_id(self):
+        return int(self.value(tag="videocodecid", first=True))
+
+    @property
+    def frame_rate(self):
+        return float(self.value(tag="framerate", first=True))
+
+    @property
+    def video_data_rate(self):
+        return int(self.value(tag="videodatarate", first=True))
+
+    @property
+    def data_size(self):
+        return int(self.value(tag="datasize", first=True))
+
+    @property
+    def audio_size(self):
+        return int(self.value(tag="audiosize", first=True))
+
+    @property
+    def video_size(self):
+        return int(self.value(tag="videosize", first=True))
+
+    @property
+    def file_size(self):
+        return int(self.value(tag="filesize", first=True))
+
+    @property
+    def last_timestamp(self):
+        return float(self.value(tag="last_timestamp", first=True))
+
+    @property
+    def last_video_frame_timestamp(self):
+        return float(self.value(tag="lastvideoframetimestamp", first=True))
+
+    @property
+    def last_key_frame_timestamp(self):
+        return float(self.value(tag="lastkeyframetimestamp", first=True))
+
+    @property
+    def last_key_frame_location(self):
+        return float(self.value(tag="lastkeyframelocation", first=True))
+
+    @property
+    def key_frames(self):
+        return KeyFrame().all_from_xml(self.metadata)
+
+    def value(self, tag=None, first=False):
+        if tag is None:
+            raise NameError("Please provide tag should not be None")
+        target = self.metadata.first(tag)
+        return "".join(target.contents) if first else self.metadata(tag)
+
+    def extract_meta(self, flv_path):   
+        temp_file = tempfile.NamedTemporaryFile(mode="w+", delete=False)
+        os.system("yamdi -i {flv_path} -x {temp_file}".format(flv_path=flv_path, temp_file=temp_file.name))
+        temp = open(temp_file.name, "rb")
+        contents = temp.readlines()
+        temp.close()
+        temp_file.close()
+        os.unlink(temp_file.name)
+        return ''.join(contents)
+
+    def boolean(self, text):
+        if "true" in text: return True
+        elif "false" in text: return False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.