From fce9e091434a09548f03aff4761a2ed8ec7a777e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Ribeiro?= Date: Mon, 5 Nov 2012 16:34:12 -0200 Subject: [PATCH] Fix memory leak and remove sh dependency (refs amoffat/sh#99) --- hlsclient/transcode.py | 12 ++++++++---- setup.py | 1 - tests/test_consumer.py | 4 ++-- tests/test_transcode.py | 16 +++++++++++----- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/hlsclient/transcode.py b/hlsclient/transcode.py index ba81017..61b5c65 100644 --- a/hlsclient/transcode.py +++ b/hlsclient/transcode.py @@ -3,7 +3,7 @@ import os import m3u8 -import sh +from subprocess import Popen DEFAULT_VIDEO_ARGS = "-f mpegts -acodec libfaac -ar 48000 -ab 64k -vcodec libx264 -flags +loop -cmp +chroma -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -maxrate 96k -bufsize 96k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -g 30".split(' ') @@ -49,8 +49,10 @@ def new_chunk_path(path, output_stream): return path.replace('.ts', '.aac') def transcode(src, output): - args = ["-y"] - args += ["-threads", len(output) * 4] + args = ["ffmpeg"] + args += ["-y"] + args += ["-loglevel", "quiet"] + args += ["-threads", str(len(output) * 4)] args += ["-i", src] for output_file in output: if output_file["type"] == "audio": @@ -67,5 +69,7 @@ def transcode(src, output): args += [output_file["path"]] else: raise NotImplementedError("Unsupported type") + logging.debug('Calling FFMPEG with args={args}'.format(args=' '.join(map(str, args)))) - sh.ffmpeg(args) + + Popen(args).communicate() diff --git a/setup.py b/setup.py index acce4c6..8a8bda4 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,5 @@ 'futures==2.1.3', 'm3u8>=0.1.1', 'pycrypto>=2.5', - 'sh>=1.03' ], ) diff --git a/tests/test_consumer.py b/tests/test_consumer.py index 7f3acc5..fe6293e 100644 --- a/tests/test_consumer.py +++ b/tests/test_consumer.py @@ -276,8 +276,8 @@ def test_consume_from_balancer_should_transcode_to_audio(tmpdir): 'output': {'audio': { "transcode": { "path": "transcode.m3u8", - "audio-bitrate": 64000, - "bandwidth": 65000 + "audio-bitrate": "64000", + "bandwidth": "65000" } }}}]} diff --git a/tests/test_transcode.py b/tests/test_transcode.py index 1a07797..af89530 100644 --- a/tests/test_transcode.py +++ b/tests/test_transcode.py @@ -1,12 +1,12 @@ from xml.dom.minidom import parseString -import sh +from subprocess import Popen, PIPE from hlsclient.transcode import transcode def get_media_info(path): - info = sh.mediainfo(str(path), "--Output=XML") - return parseString(str(info)) + result = Popen(["mediainfo", str(path), "--Output=XML"], stdout=PIPE).communicate() + return parseString(str(result[0])) def get_xml_tag_text_value(node, tag): return node.getElementsByTagName(tag)[0].firstChild.data @@ -14,6 +14,7 @@ def get_xml_tag_text_value(node, tag): def test_extracts_audio_from_ts(tmpdir): output_path = tmpdir.join("output.aac") transcode(src="tests/data/sample.ts", output=[{"path": str(output_path), "type": "audio"}]) + assert output_path.check() info = get_media_info(str(output_path)) @@ -25,15 +26,20 @@ def test_extracts_audio_from_ts(tmpdir): def test_transcode_video_and_audio_from_ts(tmpdir): audio_output_path = tmpdir.join("output.aac") video_output_path = tmpdir.join("tvglobo_200.ts") + transcode(src="tests/data/sample.ts", output=[ {"path": str(video_output_path), "type": "video", - "video-bitrate": 100000, + "video-bitrate": "100000", "size": "32x24", }, - {"path": str(audio_output_path), "type": "audio"} + {"path": str(audio_output_path), + "type": "audio"} ]) + assert audio_output_path.check() + assert video_output_path.check() + audio_info = get_media_info(str(audio_output_path)) file_track, audio_track = audio_info.getElementsByTagName("track") assert 'Audio Data Transport Stream' == get_xml_tag_text_value(file_track, "Format_Info")