From 3244793336f39430a2213b4cd762c907f9cba604 Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 29 Nov 2013 23:42:27 +0100 Subject: [PATCH] ffmpeg cmdline generation --- server/forge.py | 30 +++++++++++++++++++++++++++--- server/test_forge.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/server/forge.py b/server/forge.py index 7aba484..626c372 100644 --- a/server/forge.py +++ b/server/forge.py @@ -41,13 +41,37 @@ def get_files_and_intervals(start, end, rounder=round_timefile): start = begin + timedelta(hours=1) -def mp3_join(named_intervals): +def mp3_join(named_intervals, target): ''' Note that these are NOT the intervals returned by get_files_and_intervals, as they do not supply a filename, but only a datetime. What we want in input is basically the same thing, but with get_timefile() applied on the first element + + This function make the (quite usual) assumption that the only start_cut (if + any) is at the first file, and the last one is at the last file ''' + ffmpeg = 'ffmpeg' # binary name + startskip = None + endskip = None + files = [] for (filename, start_cut, end_cut) in named_intervals: - pass - raise NotImplementedError() + # this happens only one time, and only at the first iteration + if start_cut: + assert startskip is None + startskip = start_cut + # this happens only one time, and only at the first iteration + if end_cut: + assert endskip is None + endskip = end_cut + assert '|' not in filename + files.append(filename) + + cmdline = [ffmpeg, '-i', 'concat:%s' % '|'.join(files), '-codec:a', + 'copy'] + if startskip is not None: + cmdline += ['-ss', str(startskip)] + if endskip is not None: + cmdline += ['-to', str(len(files)*3600 - endskip)] + cmdline += [target] + return cmdline diff --git a/server/test_forge.py b/server/test_forge.py index e843ae4..42b674d 100644 --- a/server/test_forge.py +++ b/server/test_forge.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta from nose.tools import raises, assert_items_equal, eq_ from forge import get_files_and_intervals, get_timefile_exact, round_timefile,\ - get_timefile + get_timefile, mp3_join eight = datetime(2014, 5, 30, 20) nine = datetime(2014, 5, 30, 21) @@ -152,3 +152,35 @@ def test_intervals_left_2(): eq_(res[0][2], 0) eq_(res[1][1], 0) eq_(res[1][2], 3599) + +# MP3 Join + + +def test_mp3_1(): + eq_(' '.join(mp3_join((('a', 0, 0),), 'foo.mp3')), + 'ffmpeg -i concat:a -codec:a copy foo.mp3') + + +def test_mp3_1_left(): + eq_(' '.join(mp3_join((('a', 160, 0),), 'foo.mp3')), + 'ffmpeg -i concat:a -codec:a copy -ss 160 foo.mp3') + + +def test_mp3_1_right(): + eq_(' '.join(mp3_join((('a', 0, 1600),), 'foo.mp3')), + 'ffmpeg -i concat:a -codec:a copy -to 2000 foo.mp3') + + +def test_mp3_1_leftright(): + eq_(' '.join(mp3_join((('a', 160, 1600),), 'foo.mp3')), + 'ffmpeg -i concat:a -codec:a copy -ss 160 -to 2000 foo.mp3') + + +def test_mp3_2(): + eq_(' '.join(mp3_join((('a', 0, 0), ('b', 0, 0)), 'foo.mp3')), + 'ffmpeg -i concat:a|b -codec:a copy foo.mp3') + + +def test_mp3_2_leftright(): + eq_(' '.join(mp3_join((('a', 1000, 0), ('b', 0, 1600)), 'foo.mp3')), + 'ffmpeg -i concat:a|b -codec:a copy -ss 1000 -to 5600 foo.mp3')