[LINUX] Remove unwanted audio+subs

Come up with a useful post-processing script? Share it here!
synci
Newbie
Newbie
Posts: 29
Joined: February 10th, 2015, 2:19 pm

Re: [LINUX] Remove unwanted audio+subs

Post by synci »

Works perfect !!! Thanks !
User avatar
sander
Release Testers
Release Testers
Posts: 9079
Joined: January 22nd, 2008, 2:22 pm

Re: [LINUX] Remove unwanted audio+subs

Post by sander »

I discovered and fixed a bug. Please update.
synci
Newbie
Newbie
Posts: 29
Joined: February 10th, 2015, 2:19 pm

Re: [LINUX] Remove unwanted audio+subs

Post by synci »

Okay, thanks
synci
Newbie
Newbie
Posts: 29
Joined: February 10th, 2015, 2:19 pm

Re: [LINUX] Remove unwanted audio+subs

Post by synci »

That change you made (streamnumber) doesnt work :(
I switched back to the "cmd = 'mkvmerge -o "' + cleanmoviename + '" -a 1 --nosubs "' + mkvfilename + '"'" method and it works fine ?

This is my sab script log:

Code: Select all

           Step 1
Directory is /media/4fb18d2a-662b-4868-bc60-c0d6e5001b25/DATA/_DOWNLOAD/serien/tv/The.Walking.Dead.S03E01.Seed.GERMAN.DUBBED.720p.WebHD.h264-euHD
Step 2
Language ger to be kept
Searching: /media/4fb18d2a-662b-4868-bc60-c0d6e5001b25/DATA/_DOWNLOAD/serien/tv/The.Walking.Dead.S03E01.Seed.GERMAN.DUBBED.720p.WebHD.h264-euHD
/media/4fb18d2a-662b-4868-bc60-c0d6e5001b25/DATA/_DOWNLOAD/serien/tv/The.Walking.Dead.S03E01.Seed.GERMAN.DUBBED.720p.WebHD.h264-euHD/euhd-walking-s03e01-720p.mkv
cmd is ffmpeg -i "/media/4fb18d2a-662b-4868-bc60-c0d6e5001b25/DATA/_DOWNLOAD/serien/tv/The.Walking.Dead.S03E01.Seed.GERMAN.DUBBED.720p.WebHD.h264-euHD/euhd-walking-s03e01-720p.mkv" 2>&1 | grep -i -e Audio:
    Stream #0.1(ger): Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s (default)
Language found in stream  Audio
Go on
cleanmoviename /media/4fb18d2a-662b-4868-bc60-c0d6e5001b25/DATA/_DOWNLOAD/serien/tv/The.Walking.Dead.S03E01.Seed.GERMAN.DUBBED.720p.WebHD.h264-euHD/cleanedmovie.mkv
command is mkvmerge -o "/media/4fb18d2a-662b-4868-bc60-c0d6e5001b25/DATA/_DOWNLOAD/serien/tv/The.Walking.Dead.S03E01.Seed.GERMAN.DUBBED.720p.WebHD.h264-euHD/cleanedmovie.mkv" -a  Audio  --nosubs "/media/4fb18d2a-662b-4868-bc60-c0d6e5001b25/DATA/_DOWNLOAD/serien/tv/The.Walking.Dead.S03E01.Seed.GERMAN.DUBBED.720p.WebHD.h264-euHD/euhd-walking-s03e01-720p.mkv"
mkvmerge v7.5.0 ('Glass Culture') 64bit built on Jan  4 2015 17:21:29
Error: Invalid track ID in '-a Audio'.
Cleaning up
Traceback (most recent call last):
  File "/home/sabnzbd/.sabnzbd/scripts/mkv-process-audio-and-subs.py", line 60, in <module>
    handlemkv(fullname)
  File "/home/sabnzbd/.sabnzbd/scripts/mkv-process-audio-and-subs.py", line 43, in handlemkv
    os.rename(cleanmoviename, mkvfilename)
OSError: [Errno 2] No such file or directory
User avatar
sander
Release Testers
Release Testers
Posts: 9079
Joined: January 22nd, 2008, 2:22 pm

Re: [LINUX] Remove unwanted audio+subs

Post by sander »

The problem is seen here in the log you posted:

Code: Select all

    Stream #0.1(ger): Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s (default)
Language found in stream  Audio
That should be a number.

The ffmpeg output line that works in my code:

Code: Select all

Stream #0:1(ger): Audio: ac3, 48000 Hz, stereo, fltp, 224 kb/s (default)
Your line with ffmpeg output, that does not work in my code:

Code: Select all

Stream #0.1(ger): Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s (default)
... so the difference is the #0:1 versus #0.1 ... the : versus .
:-[

Solution: a regular expression

Code: Select all

re.findall(r"[\w']+", thisline)[2]
Proof that that works:

Code: Select all

>>> re.findall(r"[\w']+",  'Stream #0:1(ger): Audio: ac3, 48000 Hz, stereo, fltp, 224 kb/s (default)' )[2]
'1'
>>> re.findall(r"[\w']+",  'Stream #0.1(ger): Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s (default)' )[2]
'1'
>>>

I'll put that in the code
User avatar
sander
Release Testers
Release Testers
Posts: 9079
Joined: January 22nd, 2008, 2:22 pm

Re: [LINUX] Remove unwanted audio+subs

Post by sander »

synci
Newbie
Newbie
Posts: 29
Joined: February 10th, 2015, 2:19 pm

Re: [LINUX] Remove unwanted audio+subs

Post by synci »

Wonderfull, thanks !
synci
Newbie
Newbie
Posts: 29
Joined: February 10th, 2015, 2:19 pm

Re: [LINUX] Remove unwanted audio+subs

Post by synci »

Got some problems with permissions after mkv got finaly renamed back to original name. (OMV System with aufs pool)
So i added a chmod option, works fine:

Code: Select all

# Delete original mkv, rename new one to original name and chmod it
	print "Cleaning up"
	os.remove(mkvfilename)
	os.rename(cleanmoviename, mkvfilename)
	os.chmod(mkvfilename, 0777)
Maybe others can also need it.
Have no experience with github, so no idea how to add something like that, sorry :)

*EDIT*
I think i got pulled on your fork :)
schmitty
Newbie
Newbie
Posts: 4
Joined: May 8th, 2019, 11:11 am

Re: [LINUX] Remove unwanted audio+subs

Post by schmitty »

Hi,

I have made a few alterations to the script. One thing I would like to know is, if it is possible to set the audio track name to

Code: Select all

languageName - codec channels 
eg. English - Dolby Digital 2.0

and also set the container title to the movie title or episode number and title
User avatar
sander
Release Testers
Release Testers
Posts: 9079
Joined: January 22nd, 2008, 2:22 pm

Re: [LINUX] Remove unwanted audio+subs

Post by sander »

schmitty wrote: May 9th, 2019, 12:16 am Hi,

I have made a few alterations to the script.
Nice. Which ones?

schmitty wrote: May 9th, 2019, 12:16 am One thing I would like to know is, if it is possible to set the audio track name to

Code: Select all

languageName - codec channels 
eg. English - Dolby Digital 2.0

and also set the container title to the movie title or episode number and title
You can try on the CLI with ffmpeg. If that works, you can put it in the script.

In other words: show how you do that with ffmpeg. Then I can help with the script
schmitty
Newbie
Newbie
Posts: 4
Joined: May 8th, 2019, 11:11 am

Re: [LINUX] Remove unwanted audio+subs

Post by schmitty »

Code: Select all

#!/usr/bin/env python
### NZBGET POST-PROCESSING SCRIPT
import os
import sys
import re

## Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94

language = 'eng' # 3 letters, lower case!
languageName = 'English' # Use in track titles

def handlemkv(mkvfilename):
	print mkvfilename
	# find Audio streams 	
	# ffmpeg -i *.mkv 2>&1 | grep -i -e Audio:
	# Example output:
	#    Stream #0:1(ger): Audio: ac3, 48000 Hz, stereo, fltp, 224 kb/s (default)
	#    Stream #0:2(jpn): Audio: ac3, 48000 Hz, stereo, fltp, 224 kb/s
	cmd = 'ffmpeg -i "' + mkvfilename + '" 2>&1 | grep -i -e Audio:'
	print "cmd is", cmd
	streamnumber = -1
	for thisline in os.popen(cmd).readlines():
		print thisline.rstrip()
		if thisline.find(language)>=0:
			try:
				# streamnumber = thisline.split(':')[1].split('(')[0]
				streamnumber = re.findall(r"[\w']+", thisline)[2]
				print "Stream number", streamnumber
				streamnumber = int(streamnumber)	# force it to be an integer so we know we found correct info
			except:
				print "Something went wrong with find the stream number. Please report with logfile"
				sys.exit(0)
			break
	if streamnumber >= 0:
		print "Language found in stream", streamnumber
	else:
		print("Language %s not found" % (language))
		return()
	print "Go on"

	# leave only one audio stream, remove all subs
	# mkvmerge -o new2.mkv -a 1 --nosubs movie-Ger-Jap-Dub.mkv
	directory, filename = os.path.split(mkvfilename)
	cleanmoviename = os.path.join(directory, "cleanedmovie.mkv")
	print "cleanmoviename", cleanmoviename
	cmd = 'mkvmerge -o "' + cleanmoviename + '" --edit track:v1 + ' --delete name + '  -a ' + str(streamnumber) + ' --edit track:a1 + ' --delete name + ' --edit track:s1 + ' --delete name + ' --track-name=languageName + ' --no-global-tags + ' --no-attachments ' "' + mkvfilename + '"'
	print "command is", cmd
	for thisline in os.popen(cmd).readlines():
		print thisline.rstrip()

	# Delete original mkv, rename new one to original name and chmod it
	print "Cleaning up"
	os.remove(mkvfilename)
	os.rename(cleanmoviename, mkvfilename)
	os.chmod(mkvfilename, 0775)



try:
	root = sys.argv[1]
except:
	print "Define directory on the commandline"
	sys.exit(1)

print("Language %s to be kept" % (language))

print "Searching:", root
for path, subdirs, files in os.walk(root):
    for name in files:
        fullname = os.path.join(path, name)
	if os.path.splitext(fullname)[1].lower() == '.mkv':
		print "Found mkv", fullname
		handlemkv(fullname)
I basically added the languageName variable, and some CLI flags to the mkvmerge operation
schmitty
Newbie
Newbie
Posts: 4
Joined: May 8th, 2019, 11:11 am

Re: [LINUX] Remove unwanted audio+subs

Post by schmitty »

ffprobe "/gdrive/movies/Despicable Me (2010)/Despicable Me (2010) - 2D.AVC.h264.DTS-HD MA [5.1].Bluray-1080p.mkv"
Stream #0:1(eng): Audio: dts (DTS-HD MA), 48000 Hz, 5.1(side), s32p (24 bit) (default)
Metadata:
title : English - DTS-HD MA 5.1
BPS-eng : 3963126
DURATION-eng : 01:34:41.686000000
NUMBER_OF_FRAMES-eng: 532658
NUMBER_OF_BYTES-eng: 2814655180
_STATISTICS_WRITING_APP-eng: mkvmerge v32.0.0 ('Astral Progressions') 64-bit
_STATISTICS_WRITING_DATE_UTC-eng: 2019-03-30 03:15:58
_STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
schmitty
Newbie
Newbie
Posts: 4
Joined: May 8th, 2019, 11:11 am

Re: [LINUX] Remove unwanted audio+subs

Post by schmitty »

Hi @sander, can you offer any assistance with this script?
User avatar
sander
Release Testers
Release Testers
Posts: 9079
Joined: January 22nd, 2008, 2:22 pm

Re: [LINUX] Remove unwanted audio+subs

Post by sander »

schmitty wrote: June 4th, 2019, 10:21 pm Hi @sander, can you offer any assistance with this script?
I cannot find motivation for this. Or ... do you offer euro's to investigate and develop it?
Post Reply