Skip to content

Commit

Permalink
add in dry run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
binhex committed Jul 11, 2017
1 parent be13c6c commit 03fd0b4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Syntax
------

```
VideoSlimmer.py --mkvmerge <path> --media <path> --lang <code> [--edit-title yes] [--delete-title yes] [--log <level>] [--version]
VideoSlimmer.py --mkvmerge <path> --media <path> --lang <code> [--edit-title yes] [--delete-title yes] [--dry-run no] [--log <level>] [--version]
```

Language codes can be found [here](http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes)
Expand All @@ -27,22 +27,22 @@ Examples

<u>Linux platform</u>
```
python2 VideoSlimmer.py --mkvmerge /opt/mkvtoolnix/mkvmerge --media /media/movies --lang eng
python2 VideoSlimmer.py --mkvmerge /opt/mkvtoolnix/mkvmerge --media /media/movies --lang eng --dry-run no
```

<u>Windows platform</u>
```
VideoSlimmer.py --mkvmerge "c:\Program Files\mkvtoolnix\mkvmerge.exe" --media D:\media\movies --lang eng --edit-title yes
VideoSlimmer.py --mkvmerge "c:\Program Files\mkvtoolnix\mkvmerge.exe" --media D:\media\movies --lang eng --dry-run no --edit-title yes
```
or specifying UNC path
```
VideoSlimmer.py --mkvmerge "c:\Program Files\mkvtoolnix\mkvmerge.exe" --media \\medaserver\media\movies --lang eng --delete-title yes
VideoSlimmer.py --mkvmerge "c:\Program Files\mkvtoolnix\mkvmerge.exe" --media \\medaserver\media\movies --lang eng --dry-run no --delete-title yes
```

Notes
-----

- VideoSlimmer will NOT remove audio or subtitles unless there is a match for the specified language.
- VideoSlimmer will NOT remove audio or subtitles unless there is a match for the specified preferred language.
- VideoSlimmer IS recursive, thus all files/folders will be processed from the root defined media folder.

___
Expand Down
58 changes: 39 additions & 19 deletions VideoSlimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ def error(self, message):
sys.exit(2)

# setup argparse description and usage, also increase spacing for help to 50
commandline_parser = ArgparseCustom(prog="VideoSlimmer", description="%(prog)s " + latest_vs_version, usage="%(prog)s [--help] --mkvmerge <path> --media <path> --lang <code> [--edit-title yes] [--delete-title yes] [--log <level>] [--version]", formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=70))
commandline_parser = ArgparseCustom(prog="VideoSlimmer", description="%(prog)s " + latest_vs_version, usage="%(prog)s [--help] --mkvmerge <path> --media <path> --lang <code> [--edit-title yes] [--delete-title yes] [--dry-run yes] [--log <level>] [--version]", formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=70))

# add argparse command line flags
commandline_parser.add_argument(u"--mkvmerge", metavar=u"<path>", required=True, help=u"specify the path to mkvmerge e.g. --mkvmerge c:\Program Files\mkvtoolnix\mkvmerge.exe")
commandline_parser.add_argument(u"--media", metavar=u"<path>", required=True, help=u"specify the path to your media e.g. --media c:\media\movies")
commandline_parser.add_argument(u"--lang", metavar=u"<code>", required=True, help=u"specify the language you want to keep e.g. --lang eng")
commandline_parser.add_argument(u"--edit-title", metavar=u"yes", help=u"specify whether you want to change the title metadata to match the filename e.g. --edit-title yes")
commandline_parser.add_argument(u"--delete-title", metavar=u"yes", help=u"specify whether you want to delete the title metadata e.g. --delete-title yes")
commandline_parser.add_argument(u"--dry-run", metavar=u"no", help=u"specify whether you want to perform a dry run e.g. --dry-run yes")
commandline_parser.add_argument(u"--logpath", metavar=u"<path>", help=u"specify the path to your log files e.g. --logpath c:\videoslimmer")
commandline_parser.add_argument(u"--loglevel", metavar=u"<level>", help=u"specify the logging level, debug, info, warning, error, debug being the most verbose e.g. --loglevel info")
commandline_parser.add_argument(u"--version", action=u"version", version=latest_vs_version)
Expand Down Expand Up @@ -196,6 +197,15 @@ def error(self, message):

delete_title = False

# if enabled then perform dry run
if args["dry_run"] == "yes":

dry_run = True

else:

dry_run = False

# save log path
if args["logpath"] is None:

Expand Down Expand Up @@ -527,6 +537,10 @@ def videoslimmer():

vs_log.debug(u"filename without extension is \"%s\"" % media_file_bare)

if dry_run is True:

vs_log.info(u"[DRY RUN] No files will be modified")

# create full path to media file
media_file_path_uni = os.path.join(root, media_filename)
media_file_path_str = uni_to_byte(media_file_path_uni)
Expand Down Expand Up @@ -596,7 +610,7 @@ def videoslimmer():
elif delete_title is True:

mkvmerge_cmd += [u"--title", u""]

# check preferred audio exists, and there are audio tracks to remove
if pref_audio_list and remove_audio_list:

Expand All @@ -616,31 +630,37 @@ def videoslimmer():
file_system_encoding = sys.getfilesystemencoding()
mkvmerge_cmd = [x.encode(file_system_encoding) for x in mkvmerge_cmd]

# process file
mkvmerge_info = subprocess.Popen(mkvmerge_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
mkvmerge_info_stdout, mkvmerge_info_stderr = mkvmerge_info.communicate()
if dry_run is True:

if mkvmerge_info.returncode != 0:
vs_log.info(u"[DRY RUN] mkvmerge command is %s" % mkvmerge_cmd)

else:

vs_log.warning(u"[FAILED] output from mkvmerge is %s" % (byte_to_uni(mkvmerge_info_stdout)))
# process file
mkvmerge_info = subprocess.Popen(mkvmerge_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
mkvmerge_info_stdout, mkvmerge_info_stderr = mkvmerge_info.communicate()

if os.path.exists(temp_file_path_str):
if mkvmerge_info.returncode != 0:

# if failed then delete temp file
os.remove(temp_file_path_str)
vs_log.debug(u"deleted temporary file %s" % temp_file_uni)
vs_log.warning(u"[FAILED] output from mkvmerge is %s" % (byte_to_uni(mkvmerge_info_stdout)))

vs_log.warning(u"[FAILED] mkvmerge returned non zero return code")
continue
if os.path.exists(temp_file_path_str):

# if failed then delete temp file
os.remove(temp_file_path_str)
vs_log.debug(u"deleted temporary file %s" % temp_file_uni)

vs_log.warning(u"[FAILED] mkvmerge returned non zero return code")
continue

vs_log.debug(u"mkvmerge returned success")
vs_log.debug(u"mkvmerge returned success")

# remove source file and rename temp
os.remove(media_file_path_str)
vs_log.debug(u"removed source file \"%s\"" % media_file_path_uni)
# remove source file and rename temp
os.remove(media_file_path_str)
vs_log.debug(u"removed source file \"%s\"" % media_file_path_uni)

os.rename(temp_file_path_str, media_file_path_str)
vs_log.debug(u"renamed temporary file from \"%s\" to \"%s\"" % (temp_file_uni, media_filename))
os.rename(temp_file_path_str, media_file_path_str)
vs_log.debug(u"renamed temporary file from \"%s\" to \"%s\"" % (temp_file_uni, media_filename))

vs_log.info(u"[SUCCESS] Processing finished")

Expand Down

0 comments on commit 03fd0b4

Please sign in to comment.