this sucks man

This commit is contained in:
Hamcha 2024-04-27 16:47:50 +02:00
parent 0a8fe93297
commit a33ec56474
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
3 changed files with 61 additions and 21 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
zig-out
zig-cache
target

View file

@ -4,15 +4,7 @@ const std = @import("std");
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{

View file

@ -5,6 +5,11 @@ const TrackInfo = @import("trackinfo.zig");
const TrackMap = std.StringHashMap(TrackInfo);
const TrackLocationInfo = struct {
path: []const u8,
ext: ?[]const u8,
};
fn parse_musicdef(allocator: mem.Allocator, tracks: *TrackMap, file: []const u8) !void {
var lumps = mem.tokenizeSequence(u8, file, "Lump ");
@ -39,16 +44,17 @@ pub fn main() !void {
defer _ = gpa.deinit();
// Get args
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
//const args = try std.process.argsAlloc(allocator);
//defer std.process.argsFree(allocator, args);
if (args.len < 2) {
std.debug.print("Usage: {s} <path to extracted pk3>\n", .{args[0]});
std.process.exit(1);
}
//if (args.len < 2) {
// std.debug.print("Usage: {s} <path to extracted pk3>\n", .{args[0]});
// std.process.exit(1);
//}
// Extract folder from argv
const folder = args[1];
// const folder = args[1];
const folder = "../music";
// Convert folder to absolute
const dir = try fs.cwd().openDir(folder, .{
@ -56,7 +62,7 @@ pub fn main() !void {
});
// Prepare hashmap for storing song locations
var trackLocations = std.StringHashMap([]const u8).init(allocator);
var trackLocations = std.StringHashMap(TrackLocationInfo).init(allocator);
defer trackLocations.deinit();
var trackInfos = TrackMap.init(allocator);
@ -76,7 +82,7 @@ pub fn main() !void {
}
// Remove extension
const extIndex = mem.indexOf(u8, entry.basename, ".");
const extIndex = mem.indexOfScalar(u8, entry.basename, '.');
const filename = if (extIndex) |index| entry.basename[0..index] else entry.basename;
// Check if it's a music definition file (and parse it if so)
@ -86,7 +92,10 @@ pub fn main() !void {
try parse_musicdef(keyAllocator, &trackInfos, filedata);
} else {
// Save file to hashmap of resolved files
try trackLocations.put(try keyAllocator.dupe(u8, filename), try keyAllocator.dupe(u8, entry.path));
try trackLocations.put(try keyAllocator.dupe(u8, filename), TrackLocationInfo{
.path = try keyAllocator.dupe(u8, entry.path),
.ext = if (extIndex) |index| try keyAllocator.dupe(u8, entry.basename[index + 1 ..]) else null,
});
}
}
@ -126,10 +135,48 @@ pub fn main() !void {
const title = try slugify(allocator, trackInfo.title, trackInfo.id);
defer allocator.free(title);
const filename = try std.fmt.allocPrint(allocator, "{s} - {s}.ogg", .{ author, title });
const filename = try std.fmt.allocPrint(allocator, "{s} - {s}.{s}", .{ author, title, location.ext orelse "ogg" });
defer allocator.free(filename);
try dir.copyFile(location, targetDir, filename, .{});
// Copy the originals with ffmpeg so we can add metadata
if (trackInfo.is_original) {
const originalPath = try dir.realpathAlloc(allocator, location.path);
defer allocator.free(originalPath);
const targetPath = try targetDir.realpathAlloc(allocator, ".");
defer allocator.free(targetPath);
const finalPath = try std.fs.path.join(allocator, &[_][]const u8{ targetPath, filename });
defer allocator.free(finalPath);
const argAllocator = arenaAllocator.allocator();
//ffmpeg -i out.mp3 -metadata title="The Title You Want" -metadata artist="" -metadata album="Name of the Album" -c:a copy out2.mp3
const argv = [_][]const u8{
"ffmpeg",
"-i",
originalPath,
"-c:a",
"copy",
"-metadata",
try std.fmt.allocPrint(argAllocator, "title={s}", .{trackInfo.title orelse ""}),
"-metadata",
try std.fmt.allocPrint(argAllocator, "artist={s}", .{trackInfo.author orelse ""}),
"-metadata",
try std.fmt.allocPrint(argAllocator, "album={s}", .{trackInfo.source orelse ""}),
finalPath,
};
const proc = try std.ChildProcess.run(.{
.allocator = allocator,
.argv = &argv,
});
defer allocator.free(proc.stdout);
defer allocator.free(proc.stderr);
} else {
try dir.copyFile(location.path, targetDir, filename, .{});
}
}
}
}