Copy source filename to destination file during encode job

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
simonlefisch
Posts: 16
Joined: Mon May 08, 2017 5:35 pm

Copy source filename to destination file during encode job

Post by simonlefisch »

Description of problem or question:
Hello everyone,

I recently started to get into scripting for my encodes. As I am new to this, I found an example that I could use and it seems to work well. The issue I'm having is this...the first encode job runs and produces the file encoded1.mp4, but when the second encode job runs it uses the same output filename (encoded1.mp4). I would like the script to be able to copy the source filename to the destination filename so I can encode multiple episodes with this script. Can anyone help me out with this? I am currently learning more about scripting but have just started, so hopefully someone can take a look and help me out with this. It would be greatly appreciated.


Steps to reproduce the problem (If Applicable):




HandBrake version (e.g., 1.0.0):
Compiled from source

Code: Select all

user@server:~$ handbrake --version
Cannot load libnvidia-encode.so.1
Cannot load libnvidia-encode.so.1
Cannot load libnvidia-encode.so.1
Cannot load libnvidia-encode.so.1
[11:42:46] hb_init: starting libhb thread
[11:42:46] thread 7fd039cc4700 started ("libhb")
HandBrake 20200422135042-cd25206-master

HandBrake has exited.


Operating system and version (e.g., Ubuntu 16.04 LTS, macOS 10.13 High Sierra, Windows 10 Creators Update):
Debian 10.3 (headless)



Script

Link to script

Code: Select all

#!/bin/bash

SRC="/home/user/data/file_storage/user/Movie Rips/vids"
DEST="/home/user/data/file_storage/user/encoded_media/farscape"
DEST_EXT=mp4
HANDBRAKE=HandBrakeCLI
PRESETFILE="/home/user/data/file_storage/user/Handbrake Presets/v2_h264_sdmovies.json"
PRESET="v2_h264_sdmovies"

for FILE in "$SRC"/*
do
    filename=$(basename $FILE)
    extension=${filename##*.}
    filename=${filename%.*}

    $HANDBRAKE -i "$FILE" -o "$DEST"/"$filename".$DEST_EXT --preset-import-file "$PRESETFILE" --preset "$PRESET"

done
Woodstock
Veteran User
Posts: 3760
Joined: Tue Aug 27, 2013 6:39 am

Re: Copy source filename to destination file during encode job

Post by Woodstock »

Here is an example of a Windows CMD script that does something like you're looking for. It is called with the name of the source file, asks the interpreter to give it the file name without the extension, then plugs things in to make two copies of the source, one as an MKV file, one as an MP4, and finally directs the log information into a log named after the source:

Code: Select all

title %time% "%~n1 MKV" & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Star Trek\Deep Space 9\%~n1.mkv" -t 1 -o "V:\Star Trek\Deep Space 9\%~n1.mkv"  -f mkv --detelecine --decomb="bob" --strict-anamorphic  -e x264 -q 20 --cfr  -a 1,1,2 -E ffaac,copy:ac3,copy:ac3 -B 160,0,0 -6 dpl2,auto,auto -R Auto,Auto,Auto -D 0,0,0 --gain=0,0,0 --audio-copy-mask none --audio-fallback ffac3 --subtitle 1,1 --subtitle-forced=1 --markers="C:\Users\jeffb\AppData\Local\Temp\%~n1-1-chapters.csv" -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1 --no-dvdnav    2>>"Z:\%~n1.log"
title %time% "%~n1 M4V" & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Star Trek\Deep Space 9\%~n1.mkv" -t 1 -o "V:\Star Trek\Deep Space 9\%~n1.m4v"  -f mp4 --detelecine --decomb="bob" --strict-anamorphic  -e x264 -q 20 --cfr  -a 1,1,2 -E ffaac,copy:ac3,copy:ac3 -B 160,0,0 -6 dpl2,auto,auto -R Auto,Auto,Auto -D 0,0,0 --gain=0,0,0 --audio-copy-mask none --audio-fallback ffac3 --subtitle 1 --subtitle-forced=1 --markers="C:\Users\jeffb\AppData\Local\Temp\%~n1-1-chapters.csv" -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1 --no-dvdnav      2>>"Z:\%~n1.log"
simonlefisch
Posts: 16
Joined: Mon May 08, 2017 5:35 pm

Re: Copy source filename to destination file during encode job

Post by simonlefisch »

Woodstock wrote: Mon May 11, 2020 7:05 pm Here is an example of a Windows CMD script that does something like you're looking for. It is called with the name of the source file, asks the interpreter to give it the file name without the extension, then plugs things in to make two copies of the source, one as an MKV file, one as an MP4, and finally directs the log information into a log named after the source:

Code: Select all

title %time% "%~n1 MKV" & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Star Trek\Deep Space 9\%~n1.mkv" -t 1 -o "V:\Star Trek\Deep Space 9\%~n1.mkv"  -f mkv --detelecine --decomb="bob" --strict-anamorphic  -e x264 -q 20 --cfr  -a 1,1,2 -E ffaac,copy:ac3,copy:ac3 -B 160,0,0 -6 dpl2,auto,auto -R Auto,Auto,Auto -D 0,0,0 --gain=0,0,0 --audio-copy-mask none --audio-fallback ffac3 --subtitle 1,1 --subtitle-forced=1 --markers="C:\Users\jeffb\AppData\Local\Temp\%~n1-1-chapters.csv" -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1 --no-dvdnav    2>>"Z:\%~n1.log"
title %time% "%~n1 M4V" & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Star Trek\Deep Space 9\%~n1.mkv" -t 1 -o "V:\Star Trek\Deep Space 9\%~n1.m4v"  -f mp4 --detelecine --decomb="bob" --strict-anamorphic  -e x264 -q 20 --cfr  -a 1,1,2 -E ffaac,copy:ac3,copy:ac3 -B 160,0,0 -6 dpl2,auto,auto -R Auto,Auto,Auto -D 0,0,0 --gain=0,0,0 --audio-copy-mask none --audio-fallback ffac3 --subtitle 1 --subtitle-forced=1 --markers="C:\Users\jeffb\AppData\Local\Temp\%~n1-1-chapters.csv" -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1 --no-dvdnav      2>>"Z:\%~n1.log"
Thanks for the response @woodstock. I'm a little confused though as to how I would add that to the script I'm using. Can you point me in the right direction? Again, I'm a noob at scripting but I do have the willingness to learn. I did take a Python class in 2014 which dealt with some scripting, but that was a while ago.
Woodstock
Veteran User
Posts: 3760
Joined: Tue Aug 27, 2013 6:39 am

Re: Copy source filename to destination file during encode job

Post by Woodstock »

I was a bit in a hurry earlier, and wasn't quite getting what you needed.

I think what you're actually lacking is a test to see if you're dealing with a file for processing. Your for statement will pick up . and .., which will cause issues later when you apply other pieces to them. When I'm doing similar bash scripts, the first thing after obtaining the file name is to verify it IS a file, and not some directory that got caught in the file mask... Which you really should use:

Code: Select all

DIRECTORIES="/home/user/data/file_storage/user/Movie Rips/vids/"	# the directories to search
PATTERN="*.mp4* *.mkv* *.avi*"			# the files to process in them
DEST="/home/user/data/file_storage/user/encoded_media/farscape/"

for DIR in $DIRECTORIES
do
	for PAT in $PATTERN
	do
		for FILE in ${DIR}${PAT}
		do
			if [ -f $FILE ]       # regular files only
			do
At that point, you can dissect $FILE and assign $filename with confidence. Note that I put the ending / for the directory on the directories pattern, to simplify things a bit later.
Post Reply