Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
Fix memory leak and remove sh dependency (refs amoffat/sh#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioribeiro committed Nov 5, 2012
1 parent bc4a9ad commit fce9e09
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
12 changes: 8 additions & 4 deletions hlsclient/transcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')

Expand Down Expand Up @@ -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":
Expand All @@ -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()
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
'futures==2.1.3',
'm3u8>=0.1.1',
'pycrypto>=2.5',
'sh>=1.03'
],
)
4 changes: 2 additions & 2 deletions tests/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}}}]}

Expand Down
16 changes: 11 additions & 5 deletions tests/test_transcode.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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

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))
Expand All @@ -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")
Expand Down

0 comments on commit fce9e09

Please sign in to comment.