ffmpeg bash script

Status
Not open for further replies.

el133

Dabbler
Joined
Jun 2, 2016
Messages
14
Hi all,
So I've got everything setup just the way I want at this point.
I have some new users which causes Plex to transcode on the fly a lot. This will get me into trouble later on so I decided I need to convert the video files to a format ChromeCast can play directly. H264 4.2 it is.

I made a bash script but this is my first script ever. So before destroying all my files I'd like a checkup with the pro's here.

Does this make any sense to you?




for i in [[/media/*.avi]] || [[/media/*.mkv]] || [[/media/*.mp4]];
if [[ "$i" == *"TRANSCODED.mp4"* ]]; done
else
do ffmpeg -i "$i" -c:v libx264 -crf 20 -preset medium -level 4.2 -c:a aac -b:a 320k -movflags +faststart -vf scale=-2:720,format=yuv420p -threads 1 "${i%}"-TRANSCODED.mp4;
rm "$i"; done
 

shamo316

Dabbler
Joined
Jun 21, 2016
Messages
30
I use Handbrake and python on my system. Im not that good on codes, but this is the script I use and works great

Code:
#!/bin/bash

SRC="/media/watch"
DEST="/media/converted"
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI

for FILE in "$SRC"/*
do
	filename=$(basename "$FILE")
	extension=${filename##*.}
	filename=${filename%.*}
	$HANDBRAKE_CLI -i "$FILE" -o "$DEST"/"$filename.mp4" --preset-import-file "5.json" -x ":threads=20" .$DEST_EXT
done
 

SweetAndLow

Sweet'NASty
Joined
Nov 6, 2013
Messages
6,421
I would just use the Plex optimization that is built into Plex. This will use more specs though because you will keep the original.

Sent from my Nexus 5X using Tapatalk
 

el133

Dabbler
Joined
Jun 2, 2016
Messages
14
Thanks for your reply. I'm not sure if I want to go another way, kind of invested in this bash thing :)
Am realising the earlier script wont work. This one looks okay to me:

#!/usr/local/bin/bash
for i in [/media/OpenShare/Video/*.avi -o /media/OpenShare/Video/*.mkv -o /media/OpenShare/Video*.mp4 -a [ "$i" != *"TRANSCODED.mp4"* ]]; do
ffmpeg -i "$i" -c:v libx264 -crf 20 -preset medium -level 4.2 -c:a aac -b:a 320k -movflags +faststart -vf scale=-2:720,format=yuv420p -threads 1 "${i%}"-TRANSCODED.mp4;
rm "$i"; done
 

el133

Dabbler
Joined
Jun 2, 2016
Messages
14
I would just use the Plex optimization that is built into Plex. This will use more specs though because you will keep the original.

Sent from my Nexus 5X using Tapatalk
Yep, that's what I was hoping to do but somehow you can't delete the original file through Plex. I'm already over max capacity so that's not an option unfortunately..
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
Code:
#!/usr/local/bin/bash
for i in [/media/OpenShare/Video/*.avi -o /media/OpenShare/Video/*.mkv -o /media/OpenShare/Video*.mp4 -a [ "$i" != *"TRANSCODED.mp4"* ]];
do
	ffmpeg -i "$i" -c:v libx264 -crf 20 -preset medium -level 4.2 -c:a aac -b:a 320k -movflags +faststart -vf scale=-2:720,format=yuv420p -threads 1 "${i%}"-TRANSCODED.mp4 && rm "$i";
done


All I've really changed is to move the rm command such that it only executes if ffmpeg completes with a good exit status (&&). I don't know enough about the ffmpeg exit status options to state whether this will avoid deleting your original media if the encode fails.

If I were undertaking this effort, I'd skip the remove step and delete the originals only if the transcoded file looks good after manual inspection.
 

el133

Dabbler
Joined
Jun 2, 2016
Messages
14
This is the version that seems to work:

#!/usr/local/bin/bash
for i in /media/test/*/*.avi /media/test/*/*.mkv /media/test/*/*.mp4; do
if [[ "$i" != *"TRANSCODED.mp4"* ]]; then
ffmpeg -i "$i" -c:v libx264 -crf 20 -preset medium -level 4.2 -c:a aac -b:a 320k -movflags +faststart -vf scale=-2:720,format=yuv420p -threads 1 "${i%}"-TRANSCODED.mp4 && rm "$i";
rm "$i";
fi
done

Will let it loose in production (god help me).
Thanks all for the feedback. If for some reason transcoding fails and the original gets deleted it's not that big a deal.
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
You have the "rm" call in there twice. This means you'll remove once if the encode succeeds, and again regardless.
If your script is running, be careful about stopping it as stopping scripts like this sometimes requires multiple "^C" calls. The first might just stop the "ffmpeg" execution allowing the "rm" to proceed.

Snapshots are your friend.
 
Status
Not open for further replies.
Top