Skip to content

Commit

Permalink
Merge pull request #8 from seatedro/ffmpeg
Browse files Browse the repository at this point in the history
feat: add video support
  • Loading branch information
seatedro authored Sep 8, 2024
2 parents d62f9e3 + 083a2d5 commit f8dd409
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 76 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
jobs:
build:
name: Build for all targets
runs-on: ubuntu-latest
runs-on: macos-latest

steps:
- uses: actions/checkout@v4
Expand All @@ -18,6 +18,10 @@ jobs:
with:
version: 0.13.0

- name: Install FFmpeg
run: |
brew install ffmpeg
- name: Build for all targets
run: |
zig build -Drelease -Dci=true
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Zig Build and Test

on:
pull_request:
branches: [ main ]

jobs:
build-and-test:
Expand All @@ -24,19 +21,21 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: AnimMouse/setup-ffmpeg@v1

- name: Set up Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.13.0


- name: Build and Test
run: |
if [ "${{ matrix.os }}" = "windows-latest" ]; then
zig build -Drelease -Dtarget=${{ matrix.arch }}-windows test
elif [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
zig build -Drelease -Dtarget=${{ matrix.arch }}-linux test
elif [ "${{ matrix.os }}" = "macos-latest" ]; then
elif [[ "${{ matrix.os }}" == macos* ]]; then
zig build -Drelease -Dtarget=${{ matrix.arch }}-macos test
fi
shell: bash
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ zig-out/
/build-*/
/docgen_tmp/
images/
videos/
bin/
**/*.DS_Store
**/*.png
**/*.jpg
**/*.jpeg
test_output/

# Although this was renamed to .zig-cache, let's leave it here for a few
# releases to make it less annoying to work with multiple branches.
Expand Down
3 changes: 2 additions & 1 deletion bench.zig.sh
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
zig build run -- -i images/green_vagabond.jpg -o ascii.png
# zig build run -Drelease -- -i images/skull.png -o ascii.png -e -c -b 15.0
zig build run -Drelease -- -i "https://w.wallhaven.cc/full/85/wallhaven-856dlk.png" -o ascii.png -e -c --full_characters --sorted_ovr
86 changes: 52 additions & 34 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,53 @@ pub fn build(b: *std.Build) !void {
}
} else {
const target = b.standardTargetOptionsQueryOnly(.{});
try runZig(b, dep_stb, target, optimize);
try runZig(b, target, optimize, dep_stb);
}
}

fn buildCi(
fn setupExecutable(
b: *std.Build,
name: []const u8,
target: std.Target.Query,
optimize: std.builtin.OptimizeMode,
dep_stb: *std.Build.Dependency,
) !void {
link_libc: bool,
) !*std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = "asciigen",
.name = name,
.root_source_file = b.path("src/main.zig"),
.target = b.resolveTargetQuery(target),
.optimize = optimize,
.link_libc = true,
.link_libc = link_libc,
});

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));

linkFfmpeg(exe);

exe.addCSourceFile(.{ .file = b.path("stb/stb.c") });
exe.addIncludePath(dep_stb.path(""));

return exe;
}

fn linkFfmpeg(exe: *std.Build.Step.Compile) void {
exe.linkSystemLibrary2("libavformat", .{ .use_pkg_config = .force });
exe.linkSystemLibrary2("libavcodec", .{ .use_pkg_config = .force });
exe.linkSystemLibrary2("libavutil", .{ .use_pkg_config = .force });
exe.linkSystemLibrary2("libswscale", .{ .use_pkg_config = .force });
exe.linkSystemLibrary2("libswresample", .{ .use_pkg_config = .force });
}

fn buildCi(
b: *std.Build,
target: std.Target.Query,
optimize: std.builtin.OptimizeMode,
dep_stb: *std.Build.Dependency,
) !void {
const exe = try setupExecutable(b, "asciigen", target, optimize, dep_stb, true);

const target_output = b.addInstallArtifact(exe, .{
.dest_dir = .{
.override = .{
Expand All @@ -45,43 +74,31 @@ fn buildCi(
});

b.getInstallStep().dependOn(&target_output.step);

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));

exe.addCSourceFile(.{ .file = b.path("stb/stb.c") });
exe.addIncludePath(dep_stb.path(""));
}

fn runZig(
b: *std.Build,
dep_stb: *std.Build.Dependency,
target: std.Target.Query,
optimize: std.builtin.OptimizeMode,
dep_stb: *std.Build.Dependency,
) !void {
const exe = b.addExecutable(.{
.name = "asciigen",
.root_source_file = b.path("src/main.zig"),
.target = b.resolveTargetQuery(target),
.optimize = optimize,
.link_libc = true,
});

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));
const exe = try setupExecutable(
b,
"asciigen",
target,
optimize,
dep_stb,
true,
);

exe.addCSourceFile(.{ .file = b.path("stb/stb.c") });
exe.addIncludePath(dep_stb.path(""));

const exe_check = b.addExecutable(.{
.name = "asciigen-check",
.root_source_file = b.path("src/main.zig"),
.target = b.resolveTargetQuery(target),
.optimize = optimize,
});
exe_check.root_module.addImport("clap", clap.module("clap"));
exe_check.addCSourceFile(.{ .file = b.path("stb/stb.c") });
exe_check.addIncludePath(dep_stb.path(""));
const exe_check = try setupExecutable(
b,
"asciigen-check",
target,
optimize,
dep_stb,
false,
);
const check_step = b.step("check", "Run the check");
check_step.dependOn(&exe_check.step);

Expand All @@ -102,6 +119,7 @@ fn runZig(
.optimize = optimize,
.link_libc = true,
});
linkFfmpeg(unit_tests);
unit_tests.addCSourceFile(.{ .file = b.path("stb/stb.c") });
unit_tests.addIncludePath(dep_stb.path(""));
const run_unit_tests = b.addRunArtifact(unit_tests);
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.name = "asciigen",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "1.0.0",
.version = "1.0.4",

// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",
.minimum_zig_version = "0.12.0",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
Expand Down
26 changes: 20 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# ascii from image

converts an image to ascii art
converts images/video to ascii art

## installation

### pre-built binaries (SOMEONE PLEASE MAKE THIS WORK)
### i'm not doing pre-built binaries

you can download the latest release from github: [here](https://github.com/seatedro/asciigen/releases/latest)
it is getting too annoying to handle cross plat binary releases using my apple silicon mac. just build from source.

or wait till i release the web app!

### build from source

Expand Down Expand Up @@ -43,6 +45,8 @@ run the program with the following options (the default zig install directory is
- ` --disable_sort`: Prevents sorting of the ascii_chars by size.
- ` --block_size <u8>`: Set the size of the blocks. (default: 8)
- ` --threshold_disabled`: Disables the threshold.
- ` --codec <string>`: Set the encoder codec like "libx264" or "hevc_videotoolbox". (default: searches for encoders on your machine)
- ` --keep_audio`: Preserves audio from input video.

2. examples:

Expand All @@ -67,10 +71,20 @@ run the program with the following options (the default zig install directory is
asciigen -i "https://w.wallhaven.cc/full/p9/wallhaven-p9gr2p.jpg" -o output.png -e -c-b 1.5
```

3. the program will generate an ascii art version of your input image and save it as a new image file.
with an input video (no urls allowed):
```bash
asciigen -i /path/to/input/video.mp4 -o ascii.mp4 --codec hevc_nvenc --keep_audio
```

with input video and custom ffmpeg encoder options:
```bash
asciigen -i /path/to/input/video.mp4 -o ascii.mp4 -c --codec libx264 --keep_audio-- -preset fast -crf 20
```

3. the program will generate an ascii art version of your input media and save it as a new media file.

output file needs to be a `.png` since i saw some weird issues with jpegs.
for images: output file needs to be a `.png` since i saw some weird issues with jpegs.

the zig version is the only one i'll be working on from here on. the c code was just to get me started until i figured out some issues with the build.zig

4. Using the long arguments on windows may or may not work. Please use the short arguments for now.
4. using the long arguments on windows may or may not work. please use the short arguments for now.
Loading

0 comments on commit f8dd409

Please sign in to comment.