FFMPEG Script

other-dev

#1

Hi. So i wrote this script to remux all videofiles to .mp4 but i think it looks really bad. It does its job, but can i rewrite it so it runs better?

shopt -s globstar

for f in */.mov; do ffmpeg -y -i "$f" -c copy -sn "${f%.mov}.mp4"; done
for f in */.mkv; do ffmpeg -y -i "$f" -c copy -sn "${f%.mkv}.mp4"; done
for f in */.avi; do ffmpeg -y -i "$f" -c copy -sn "${f%.avi}.mp4"; done
for f in */.flv; do ffmpeg -y -i "$f" -c copy -sn "${f%.flv}.mp4"; done
for f in */.FLV; do ffmpeg -y -i "$f" -c copy -sn "${f%.FLV}.mp4"; done
find . -name '.mov' -delete
find . -name '
.mkv' -delete
find . -name '.avi' -delete
find . -name '
.flv' -delete
find . -name '*.FLV' -delete

now the script makes a duplicate of the videofile. example: remuxing a .mkv to .mp4. both videofiles is in the folder untill the end of the script where it gets deleted. I want ffmpeg to automatically detect the container and codecs. if it needs to remux then it remux, if it needs transcoding then transcode. also want the script to delete the original video file before going to the next video file. i do not know how to write like that. i found bits and pieces on the interwebz and just tried out commands.

Thanks


#2

@stohle said:
Hi. So i wrote this script to remux all videofiles to .mp4 but i think it looks really bad. It does its job, but can i rewrite it so it runs better?

Simply use Cayars redone scripts...

https://forums.plex.tv/discussion/comment/1117614/#Comment_1117614


#3

The problem with the OP getting to @cayars scripts is he posted that in a Pass only forum and the OP doesn't have a Pass.

@cayars scripts are really cool. They strip out any language you haven't designated (but it has to be in the file to begin with) and makes an MP4, H264 with stereo AAC audio for the first audio track, and if available makes a DTS or AC3 5.1 as well. For every language you designate it to do. It also makes an srt sub title file for the language you designated, too. It deletes the original file, and you can run this without an output directory, so it puts the new file into the same folder as the original. If it's called via a script if can be completely automatic.

Since this is @cayars' scripts, and not mine, I'll let him comment here if he wants to, about how to get to it. I'm not going to send 3 million people to his FTP without him clearing it first... :)


#4

It's fine Mike. Here's a copy of the original post i made in my thread which is in the plex pass part of the site:

Here is a set of scripts that you can use to automate mp4 creation of files.

I'd recommend running your files through FileBot first to rename them correctly before running them through this script. It can pull SRT files so everything ends up with proper names this way.

The only pre-requisite is to have python 2.7.9 installed. https://www.python.org/downloads/release/python-279/

(I'm running 64bit version for windows installed to C:\Python27)

This is a modified version of sickbeard_mp4_automator with modifications by me. I pulled out all the integration features to make setup much easier. I also modified it to produce higher quality MP4 files that will direct play better with Plex.

Download convert.zip from ftp://ayars.tv

username and password are both guest

Unzip to C: drive. You will end up with C:\Convert

There are two directories (can be changed) inside this folder that are used for the media

Process is where you put any files you want to convert

Done is where the files will end up after processing

You can run this multiple ways but the easiest is to use the Run.bat file.

If you edit this file you will notice it's one line that reads: c:\python27\python manual.py -a -i c:\convert\process

If you install python to a different folder then change the python directory at the beginning of this line.

If you want to process files from a different location then c:\convert\process then just change this in the batch file.

If you want to change the final location of where the final files are stored then edit autoProcess.ini file and change the output_directory setting.

That's pretty much it.

Put files in the process directory and run the batch file

What this will do:

It will process each and every file in the "process" directory.

It will remux files that are h.264 and will transcode files that aren't h.264 video

It will pull out any english subtitles and create SRT files from them. If you want to pull out other languages then modify the subtitle-language setting in the ini file. Multiple languages are supported by commas

The completed file will not have subtitles (we pulled them out)

It will clear all tags in the mp4 file so Plex doesn't pick up and use these tags instead of the proper meta-data

It will create an AAC track as the first track upto 256K in size.

It will remove all audio tracks not in english. If you want other languages included modify audio-language setting (ini file) and include any languages you want kept in the MP4 file. Multiple languages are supported by commas

When it transcodes it wil use an h.264 profile of high; a level of 4.0; crf setting of 20. These are basically the same as Handbrake High Profile settings with a bit more refinement for Plex use that allows it to direct play more often.

In all cases the final MP4 file has the MOOV ATOM at the beginning of the file (ie web optimized)

Advanced setting:

You can limit the total bitrate for the video. If for example you use a client that has an upper limit of 12000 KB you can set the option video-bitrate option to something like 11000 which gives you a bit of headroom for a couple of audio tracks.

There is more that can be done with this set of scripts but lets start off with these. :)

Let me know what you guys think,

Carlo


#5

@cayars Thank you so much! i did find a website claiming to have your script but didnt trust the site. setting it up now. I am running Debian with Sonarr, Couchpotato, Deluge and SABnzbd. I've set SAB to download to the same folders as Deluge.

Deluge download to incomplete, then moves completed to the folder completed
SAB download to the same folder and then moves it to the same folder as Deluge.
Sonarr and Couchpotato checks the "completed" folder for content and then move the files to another folder (TV-Series and Movies) which is the folders where Plex picks up content.

my question is; how should I set up this with folders and stuff so the content get remuxed or whatever before they are moved to their respective folders?

Download to "incomplete", move to "remux" folder, run script, move to "complete" where sonarr and couchpotato handle the moving to their respective folders?

i really need some guidance on this because this is new to me.

thanks for helping me out!


#6

It says "output_directory" in the autoprocess.ini file. wondering if that means that the converted file is being made in the output directory or somewhere else and then its moved when its 100% finished. dont want couchpotato and sonarr to pick up unfinished files. (sorry for my grammar. its 03:33 here)


#7

Forget whats above this reply!

@cayars its up and running now but looks like i have to manually run python manual.py and enter the path.

I changed the paths in run.bat and the output i get is:

Manual processor started.
64bit Python.

any fix for this?


#8

@stohle said:
Forget whats above this reply!

@cayars its up and running now but looks like i have to manually run python manual.py and enter the path.

I changed the paths in run.bat and the output i get is:

Manual processor started.
64bit Python.

any fix for this?

I'm a bit confused. Are you running Debian or Windows?


#9

@cayars headless Debian to be exact


#10

Ran into several issues trying to run cayars script in a 32-bit WinXP environment. Here's how I got it to run: http://jdsworld.com/tech-support/ffmpeg-script-remux-video-files-mp4-winxp/


#11

@cayars Hope all is well! Love your script :)

Is there any way to ensure there is only 1 audio stream? English AAC 2 channel 256kbit?

Basically I want to drop extra surround channels and just keep it at 2 channel.

Thanks :)


#12

Would you mind if I ask why you would want to do this? Audio is a very small part of total file size. Next month or next year you might very well wish you had more then 2 channel audio.


#13

Hi @cayers, Thanks for the reply and sorry about the delay on the reply.

I'm just working to converting everything to the most compatible file formats and what not obviously. And it seems that some players default to the 6 channel sound if it exists. When the secondary audio stream is copied is keeps the original codec. Some players (like shitty vizio TVs) will transcode by default due to this.

Also, is there a way to print the FFMpeg command for each file processed (IE in the log or console output c:\convert\ffmpeg %source %args %outputfile).

Love the script anywhich way! I've converted most of my library using it. I'll likely rerun it all if/when you can answer the above question.

Thanks again!


#14

@cayars

Why is it that sometimes it is very slow to convert a 500MB tv show file but most of the time it's very fast to convert a 1.5GB tv show file? This is on the same computer that's running the conversion.


#15

@cayars

Help?


#16

Depends on the file. Lots of reasons. For example if the encoding level is 4.0 it might just remux the file while if the encoding level was 5.0 it will transcode it to get the level to what is specified in the ini file. It can use hardware encoding if available for many but not all transcodes.

Then of course you have things like VC-1 encoding which will only do software encoding and can only use 1 core (ffmpeg limitation).

There are many, many reasons. That's why the script is so useful because it looks at the file to begin with to determine what is needed and will perform the "fastest" method.


#17

Thank you, @cayars! The script looks like what I've been hoping to find to automate transcoding new additions to my library. Will this work on an ubuntu setup? If not, any suggestions as to what might?

Also, I couldn't connect to the ftp. It seems to be down. Is there somewhere else I can grab the scripts?


#18

I was doing some house cleaning. Try FTP again.

Yes it will run on OS X, Windows and Linux.


#19

Thank you very much. The FTP worked and I'm downloading now.
Looking forward to getting this setup on my server!


#20

@stohle said:
Forget whats above this reply!

@cayars its up and running now but looks like i have to manually run python manual.py and enter the path.

I changed the paths in run.bat and the output i get is:

Manual processor started.
64bit Python.

any fix for this?

Hi @stohle - did you figure out what had to be resolved to get past this issue above?
I am having the same output logged when i run the script.

I'm running LinuxMint 18.1 and have Python 2.7.12 installed.