Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

This script is for removing unwanted audio streams or subtitle streams, or adding subtitles, and also has the capability to merge 2 files keep the video from one and the audio from the other!

Note: This script DOES NOT CONVERT anything, as that takes a lot of time and always looses quality. The point of this script is to be extremely fast. It almost certainly requires the use of FileBot FileRenamer.

It is ported from my Handbrake Recursive script.

  1. Install FFMPEG
  2. Place audio.sh in /media/audio.sh

Uncomment the line you wish to use and the file type. You can add many more "presets/FLAGS", this is just an idea.

ffmpeg.sh contents:

#!/bin/bash
#Uncomment a Preset FLAGS depending on video/subtitle/audio stream wanting to keep, easy to find which stream via playing in VLC and hitting CTRL + J (when not in fullscreen)

#TO ADD SUBTITLES; THEY MUST BE SAME NAME AS FILE "$1.srt" --*Check out Filebot Filerenamer*
#Don't Strip audio and do subtitles at the same time, do one at a time
#To add subtitles, just uncomment the line under ADD SUBTITLES

#TO FIX AUDIO SYNC
#Use hotkeys J/K in VLC to find correct value
#DONT use FLAGS, just OFFSET 

#FLAGS DESC:
#"-map 0:0 -vcodec copy" -- which video stream to copy
#"-map 0:1 -acodec copy" -- which audio stream to copy
#"-map 0:2 -scodec copy" -- which subtitle stream to copy
#"-itsoffset 0.2"	 -- DELAY Audio by 0.2 seconds can do -0.2 for opposite effect!

###Video Stream 0 Audio stream 1 Subtitle Stream 2
#export FLAGS="-map 0:0 -vcodec copy -map 0:1 -acodec copy -map 0:2 -scodec copy"

###Video Stream 0 Audio Stream 1 Aspect 16:9
#export FLAGS="-map 0:0 -vcodec copy -map 0:1 -acodec copy -aspect 16:9"

##If Aspect Doesn't Work - Rencode Aspect
##Cartoon Tuned
#export FLAGS="-c:v libx264 -preset ultrafast -crf 22 -tune animation -c:a copy -aspect 16:9"

###Video Stream 0 Audio Stream 1
#export FLAGS="-map 0:0 -vcodec copy -map 0:1 -acodec copy"

###Audio Stream 2
#export FLAGS="-map 0:0 -vcodec copy -map 0:2 -acodec copy"

###Audio Stream 3
#export FLAGS="-map 0:0 -vcodec copy -map 0:3 -acodec copy"

###Copy Audio from 2nd file w/ same name but ending in ".audio" IE: "$1.audio"
##Copy Video Stream 0 from "$1" and Subtitles Stream 2 from "$1" and Audio Stream 1 from "$1.audio" 
#export FLAGS="-c copy -map 0:0 -map 0:2 -map 1:0"

##FIX AUDIO SYNC
#export OFFSET="-itsoffset 0.2"



if [ -z "$1" ] ; then
    TRANSCODEDIR="."
else
    TRANSCODEDIR="$1"
fi

    ##To remove source files after conversion and rename back to original w/o *FINAL*, uncomment second line #&&find (Must become 1 line of code IE: Hit delete 2 times)
    ##I do not reccomend using this unless you are 100% of what you are doing, it will delete source files even if conversion fails, basically unless you are creator of files
    ##ANY files without FINAL will be deleted, be warned, I have personally deleted many a files I didnt want to by using this line :P
    ##USE ALIASES (and confirm script results) INSTEAD OF &&FIND REMOVE AUTOMATION

    #AVI
    #find "$TRANSCODEDIR"/* -type f -name "*.avi" -exec bash -c 'ffmpeg -i "$1" $FLAGS "${1%\.*}"FINAL.avi ' __ {} \; 
#&& find . -type f -not -name '*FINAL.avi' -name '*.avi' -delete && rename 's/FINAL//' *.avi
    #AUDIO SYNC
    #find "$TRANSCODEDIR"/* -type f -name "*.avi" -exec bash -c 'ffmpeg -i "$1" $OFFSET -i "$1" -map 0:v -vcodec copy -map 1:a -acodec copy -map 0:s? -scodec copy "${1%\.*}"FINAL.avi ' __ {} \;
#&& find . -type f -not -name '*FINAL.mp4' -name '*.avi' -delete && rename 's/FINAL//' *.avi
    ##INSERT AUDIO FROM "$1.audio"
    #find "$TRANSCODEDIR"/* -type f -name "*.aviv" -exec bash -c 'ffmpeg -i "$1" -i "${1%\.*}".audio $FLAGS "${1%\.*}FINAL.avi" ' __ {} \;
#&& find . -type f -not -name '*FINAL.mp4' -name '*.avi' -delete && rename 's/FINAL//' *.avi

    #MKV
    #FROM ORIGINAL STREAMS
    #find "$TRANSCODEDIR"/* -type f -name "*.mkv" -exec bash -c 'ffmpeg -i "$1" $FLAGS "${1%\.*}"FINAL.mkv ' __ {} \;
#&& find . -type f -not -name '*FINAL.mkv' -name '*.mkv' -delete && rename 's/FINAL//' *.mkv
    #ADD SUBTITLES
    #find "$TRANSCODEDIR"/* -type f -name "*.mkv" -exec bash -c 'ffmpeg -i "$1" -sub_charenc CP1252 -i "${1%\.*}".srt -vcodec copy -acodec copy "${1%\.*}"FINAL.mkv ' __ {} \;
#&& find . -type f -not -name '*FINAL.mkv' -name '*.mkv' -delete && rename 's/FINAL//' *.mkv
    #AUDIO SYNC
    #find "$TRANSCODEDIR"/* -type f -name "*.mkv" -exec bash -c 'ffmpeg -i "$1" $OFFSET -i "$1" -map 0:v -vcodec copy -map 1:a -acodec copy -map 0:s? -scodec copy "${1%\.*}"FINAL.mkv ' __ {} \;
#&& find . -type f -not -name '*FINAL.mp4' -name '*.mkv' -delete && rename 's/FINAL//' *.mkv
    ##INSERT AUDIO FROM "$1.audio"
    #find "$TRANSCODEDIR"/* -type f -name "*.mkv" -exec bash -c 'ffmpeg -i "$1" -i "${1%\.*}".audio $FLAGS "${1%\.*}FINAL.mkv" ' __ {} \;
#&& find . -type f -not -name '*FINAL.mp4' -name '*.mkv' -delete && rename 's/FINAL//' *.mkv

    #MP4
    #FROM ORIGINAL STREAMS
    #find "$TRANSCODEDIR"/* -type f -name "*.mp4" -exec bash -c 'ffmpeg -i "$1" $FLAGS "${1%\.*}"FINAL.mp4 ' __ {} \;
#&& find . -type f -not -name '*FINAL.mp4' -name '*.mp4' -delete && rename 's/FINAL//' *.mp4
    #ADD SUBTITLES
    #find "$TRANSCODEDIR"/* -type f -name "*.mp4" -exec bash -c 'ffmpeg -i "$1" -i "${1%\.*}".srt -c:v copy -c:a copy -c:s copy "${1%\.*}"FINAL.mkv ' __ {} \;
#&& find . -type f -not -name '*FINAL.mkv' -name '*.mp4' -delete && rename 's/FINAL//' *.mkv
    #AUDIO SYNC
    #find "$TRANSCODEDIR"/* -type f -name "*.mp4" -exec bash -c 'ffmpeg -i "$1" $OFFSET -i "$1" -map 0:v -vcodec copy -map 1:a -acodec copy -map 0:s? -scodec copy "${1%\.*}"FINAL.mp4 ' __ {} \;
#&& find . -type f -not -name '*FINAL.mp4' -name '*.mp4' -delete && rename 's/FINAL//' *.mp4
    ##INSERT AUDIO FROM "$1.audio"
    #find "$TRANSCODEDIR"/* -type f -name "*.mp4" -exec bash -c 'ffmpeg -i "$1" -i "${1%\.*}".audio $FLAGS "${1%\.*}FINAL.mp4" ' __ {} \;
#&& find . -type f -not -name '*FINAL.mp4' -name '*.mp4' -delete && rename 's/FINAL//' *.mp4

3. Configure Alias for Automation! This copys the script to bottom of directory and allows you to uncomment $flags and filetype, also cleanup aka mkvfinal and mkvaudio for rename batch files to $1.audio

sudo nano ~/.bashrc && sudo source ~/.bashrc 
alias ffmpegscript="sudo cp /media/ffmpeg.sh ./ && sudo nano ./ffmpeg.sh ; sudo nohup ./ffmpeg.sh > convert.nohup &"

alias mkvfinal="find . -type f -not -name '*FINAL.mkv' -name '*.mkv' -delete && rename 's/FINAL//' *.mkv && find . -name 'audio.sh' -delete && find . -name 'convert.nohup' -delete"

alias avifinal="find . -type f -not -name '*FINAL.avi' -name '*.avi' -delete && rename 's/FINAL//' *.avi && find . -name 'audio.sh' -delete && find . -name 'convert.nohup' -delete"

alias mp4final="find . -type f -not -name '*FINAL.mp4' -name '*.mp4' -delete && rename 's/FINAL//' *.mp4 && find . -name 'audio.sh' -delete && find . -name 'convert.nohup' -delete"

alias srtremove="find . -name '*.srt' -delete"

alias mp4audio="rename 's/\.mp4$/.audio/' *.mp4"
alias mkvaudio="rename 's/\.mkv$/.audio/' *.mkv"
alias m4vaudio="rename 's/\.m4v$/.audio/' *.m4v"
alias aviaudio="rename 's/\.avi$/.audio/' *.avi" 
  • No labels

1 Comment