/usr/share/pyshared/audioread/ffdec.py is in python-audioread 0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | # This file is part of audioread.
# Copyright 2011, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Read audio data using the ffmpeg command line tools via a UNIX
pipe.
"""
import subprocess
import re
class FFmpegError(Exception):
pass
class CommunicationError(FFmpegError):
"""Raised when the output of FFmpeg is not parseable."""
class UnsupportedError(FFmpegError):
"""The file could not be decoded by FFmpeg."""
class NotInstalledError(FFmpegError):
"""Could not find the ffmpeg binary."""
class FFmpegAudioFile(object):
"""An audio file decoded by the ffmpeg command-line utility."""
def __init__(self, filename):
try:
self.proc = subprocess.Popen(
['avconv', '-i', filename, '-f', 's16le', '-'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
except OSError:
raise NotInstalledError()
self._get_info()
def read_data(self, block_size=4096):
"""Read blocks of raw PCM data from the file."""
while True:
data = self.proc.stdout.read(block_size)
if not data:
break
yield data
def _get_info(self):
"""Reads the tool's output from its stderr stream, extracts the
relevant information, and parses it.
"""
out_parts = []
while True:
line = self.proc.stderr.readline()
if not line:
# EOF and data not found.
raise CommunicationError("stream info not found")
line = line.strip().lower()
if 'no such file' in line:
raise IOError('file not found')
elif 'invalid data found' in line:
raise UnsupportedError()
elif 'duration:' in line:
out_parts.append(line)
elif 'audio:' in line:
out_parts.append(line)
self._parse_info(''.join(out_parts))
break
def _parse_info(self, s):
"""Given relevant data from the ffmpeg output, set audio
parameter fields on this object.
"""
self.samplerate = int(re.search(r'(\d+) hz', s).group(1))
mode = re.search(r'hz, ([^,]+),', s).group(1)
if mode == 'stereo':
self.channels = 2
else:
self.channels = 1
durparts = re.search(
r'duration: (\d+):(\d+):(\d+).(\d)', s
).groups()
durparts = map(int, durparts)
duration = durparts[0] * 60 * 60 + \
durparts[1] * 60 + \
durparts[2] + \
float(durparts[3]) / 10
self.duration = duration
def close(self):
"""Close the ffmpeg process used to perform the decoding."""
if hasattr(self, 'proc') and self.proc.returncode is None:
self.proc.terminate()
self.proc.communicate()
def __del__(self):
self.close()
# Iteration.
def __iter__(self):
return self.read_data()
# Context manager.
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return False
|