Skip to content

Commit

Permalink
Added --limit=<seconds> flag to csv export. Only the first seconds wi…
Browse files Browse the repository at this point in the history
…ll be

processed, then substudy will stop early. Addresses feature request emk#14.

Added tests to confirm that the generated csv contains expected data.

Bumped cli_test_dir to version 0.1.2 to take advantage of new
expect_file_contents and expect_contains methods.
  • Loading branch information
Arthaey committed Jul 24, 2017
1 parent 32d4a03 commit 5abd9cc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ regex = "0.1.41"
uchardet = "2.0.0"

[dev-dependencies]
cli_test_dir = "0.1"
cli_test_dir = "0.1.2"
difference = "0.4.1"

[[bin]]
Expand Down
11 changes: 7 additions & 4 deletions src/bin/substudy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Subtitle processing tools for students of foreign languages
Usage: substudy clean <subs>
substudy combine <foreign-subs> <native-subs>
substudy export csv <video> <foreign-subs> [<native-subs>]
substudy export csv [--limit=<secs>] <video> <foreign-subs> [<native-subs>]
substudy export review <video> <foreign-subs> [<native-subs>]
substudy export tracks <video> <foreign-subs>
substudy list tracks <video>
Expand All @@ -45,6 +45,7 @@ struct Args {
arg_foreign_subs: String,
arg_native_subs: Option<String>,
arg_video: String,
flag_limit: Option<f32>,
flag_version: bool,
}

Expand Down Expand Up @@ -73,7 +74,8 @@ fn run(args: &Args) -> Result<()> {
{
cmd_export(export_type(args), &Path::new(video_path),
&Path::new(foreign_path),
native_path.as_ref().map(|p| Path::new(p)))
native_path.as_ref().map(|p| Path::new(p)),
args.flag_limit)
}
Args{cmd_tracks: true, arg_video: ref path, ..} =>
cmd_tracks(&Path::new(path)),
Expand Down Expand Up @@ -111,7 +113,8 @@ fn cmd_tracks(path: &Path) -> Result<()> {
}

fn cmd_export(kind: &str, video_path: &Path, foreign_sub_path: &Path,
native_sub_path: Option<&Path>) ->
native_sub_path: Option<&Path>,
limit: Option<f32>) ->
Result<()>
{
// Load our input files.
Expand All @@ -123,7 +126,7 @@ fn cmd_export(kind: &str, video_path: &Path, foreign_sub_path: &Path,
};

let mut exporter =
try!(export::Exporter::new(video, foreign_subs, native_subs, kind));
try!(export::Exporter::new(video, foreign_subs, native_subs, kind, limit));
match kind {
"csv" => try!(export::export_csv(&mut exporter)),
"review" => try!(export::export_review(&mut exporter)),
Expand Down
9 changes: 9 additions & 0 deletions src/export/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct AnkiNote {
/// Export the video and subtitles as a CSV file with accompanying media
/// files, for import into Anki.
pub fn export_csv(exporter: &mut Exporter) -> Result<()> {
let processing_limit = exporter.processing_limit();
let foreign_lang = exporter.foreign().language;
let prefix = episode_prefix(exporter.file_stem());

Expand Down Expand Up @@ -72,6 +73,14 @@ pub fn export_csv(exporter: &mut Exporter) -> Result<()> {
let native = ctx.map(|&(_, ref n)| n).flatten();

if let Some(curr) = foreign.curr {
match processing_limit {
Some(seconds) if curr.period.begin() > seconds => {
println!("Limiting to {:?} seconds of video.", seconds);
break;
}
_ => {}
}

let period = curr.period.grow(1.5, 1.5);

let image_path = exporter.schedule_image_export(period.midpoint());
Expand Down
12 changes: 11 additions & 1 deletion src/export/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub struct Exporter {
/// A list of media files we want to extract from our video as
/// efficiently as possible.
extractions: Vec<Extraction>,

/// The number of seconds to limit processing to.
processing_limit: Option<f32>,
}

impl Exporter {
Expand All @@ -71,7 +74,8 @@ impl Exporter {
pub fn new(video: Video,
foreign_subtitles: SubtitleFile,
native_subtitles: Option<SubtitleFile>,
label: &str) ->
label: &str,
processing_limit: Option<f32>) ->
Result<Exporter>
{
let foreign = LanguageResources::new(foreign_subtitles);
Expand Down Expand Up @@ -100,6 +104,7 @@ impl Exporter {
file_stem: file_stem,
dir: dir,
extractions: vec!(),
processing_limit: processing_limit,
})
}

Expand Down Expand Up @@ -129,6 +134,11 @@ impl Exporter {
self.native.as_ref()
}

/// Get the seconds to limit processing to.
pub fn processing_limit(&self) -> Option<f32> {
self.processing_limit
}

/// Align our two sets of subtitles.
pub fn align(&self) -> Vec<(Option<Subtitle>, Option<Subtitle>)> {
align_available_files(&self.foreign.subtitles,
Expand Down
24 changes: 24 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ fn cmd_combine() {
assert!(from_utf8(&output.stdout).unwrap().find("¡Si!").is_some());
}

static FIRST_LINE: &'static str = "[sound:empty_00060_828-00066_164.es.mp3],0:01:00.828,empty,\"<img src=\"\"empty_00063_496.jpg\"\" />\",¡Si! ¡Aang ha vuelto!,Yay! Yay! Aang\'s back!,,,¡Lo sabía!,I knew it!\n";

static LAST_LINE: &'static str = "[sound:empty_00077_124-00083_379.es.mp3],0:01:17.124,empty,\"<img src=\"\"empty_00080_251.jpg\"\" />\",\"El no ha hecho nada, Sokka, fué un accidente.\",Aang didn\'t do anything.,harás que vengan a nosotros.,\"You\'re leading them straight to us, aren\'t you?\",,\n";

#[test]
fn cmd_export_csv() {
let testdir = TestDir::new("substudy", "cmd_export_csv");
Expand All @@ -68,8 +72,28 @@ fn cmd_export_csv() {
testdir.expect_path("empty_csv/cards.csv");
testdir.expect_path("empty_csv/empty_00063_496.jpg");
testdir.expect_path("empty_csv/empty_00060_828-00066_164.es.mp3");
testdir.expect_contains("empty_csv/cards.csv", FIRST_LINE);
testdir.expect_contains("empty_csv/cards.csv", LAST_LINE);
}

#[test]
fn cmd_export_csv_limit() {
let testdir = TestDir::new("substudy", "cmd_export_csv_limit");
let output = testdir.cmd()
.args(&["export", "csv", "--limit=65"])
.arg(testdir.src_path("fixtures/empty.mp4"))
.arg(testdir.src_path("fixtures/sample.es.srt"))
.arg(testdir.src_path("fixtures/sample.en.srt"))
.output()
.expect("could not run substudy");
assert!(output.status.success());
testdir.expect_path("empty_csv/cards.csv");
testdir.expect_path("empty_csv/empty_00063_496.jpg");
testdir.expect_path("empty_csv/empty_00060_828-00066_164.es.mp3");
testdir.expect_file_contents("empty_csv/cards.csv", FIRST_LINE);
}


#[test]
fn cmd_export_review() {
let testdir = TestDir::new("substudy", "cmd_export_review");
Expand Down

0 comments on commit 5abd9cc

Please sign in to comment.