Guest User

vrcencode.py

a guest
Sep 6th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import ffmpeg
  3. import sys
  4. import fcntl
  5. import os
  6. infile, outfile = sys.argv[1:]
  7.  
  8. # try to work around dumb dual audio files
  9. probe = ffmpeg.probe(infile)
  10. audio_track = 0
  11. audio_count = 0
  12. audio_stream = None
  13. sub_type = 'text'
  14. for stream in probe['streams']:
  15.     # try to select jpn audio over english
  16.     if stream['codec_type'] == 'audio':
  17.         tags = stream['tags']
  18.         if tags and 'language' in tags and tags['language'] == 'jpn':
  19.             audio_track = audio_count
  20.             audio_stream = stream
  21.         audio_count += 1
  22.     if stream['codec_type'] == 'subtitle':
  23.         if stream['codec_name'] == 'dvd_subtitle':
  24.             sub_type = 'dvd'
  25.  
  26. print(f"selecting {audio_track=} {audio_stream=}", file=sys.stderr)
  27.  
  28. ffin = ffmpeg.input(infile)
  29. ffv = ffin.video.filter('format', 'yuv420p')
  30. # assume subtitle track matches the audio track
  31. if sub_type == 'text':
  32.     ffv = ffv.filter('subtitles', filename=infile, stream_index=audio_track)
  33. else:
  34.     # burn the dvd subs on the image
  35.     subs = ffin[f's:{audio_track}']
  36.     s2r = ffmpeg.filter_multi_output([subs, ffv], 'scale2ref')
  37.     ffv = s2r[1].overlay(s2r[0])
  38.  
  39. opts = {
  40.         'c:v': 'libx264',
  41.         'preset': 'medium',
  42.         'tune': 'animation',
  43.         'movflags': 'faststart',
  44.         'crf': '28',
  45.         'c:a': 'aac',
  46.         'b:a': '160k',
  47.         'ac': '1',
  48.         #'t': '60'
  49.         }
  50. audio = ffin[f'a:{audio_track}']
  51.  
  52. stream = ffmpeg.output(ffv, audio, outfile, **opts)
  53.  
  54. print(f"will run {' '.join(stream.get_args())=}", file=sys.stderr)
  55.  
  56. # basic flock queueing for multiple invocations
  57. with open("/tmp/vrcencode", "w+") as f:
  58.     fcntl.flock(f, fcntl.LOCK_EX)
  59.     stream.run()
Advertisement
Add Comment
Please, Sign In to add comment