Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import ffmpeg
- import sys
- import fcntl
- import os
- infile, outfile = sys.argv[1:]
- # try to work around dumb dual audio files
- probe = ffmpeg.probe(infile)
- audio_track = 0
- audio_count = 0
- audio_stream = None
- sub_type = 'text'
- for stream in probe['streams']:
- # try to select jpn audio over english
- if stream['codec_type'] == 'audio':
- tags = stream['tags']
- if tags and 'language' in tags and tags['language'] == 'jpn':
- audio_track = audio_count
- audio_stream = stream
- audio_count += 1
- if stream['codec_type'] == 'subtitle':
- if stream['codec_name'] == 'dvd_subtitle':
- sub_type = 'dvd'
- print(f"selecting {audio_track=} {audio_stream=}", file=sys.stderr)
- ffin = ffmpeg.input(infile)
- ffv = ffin.video.filter('format', 'yuv420p')
- # assume subtitle track matches the audio track
- if sub_type == 'text':
- ffv = ffv.filter('subtitles', filename=infile, stream_index=audio_track)
- else:
- # burn the dvd subs on the image
- subs = ffin[f's:{audio_track}']
- s2r = ffmpeg.filter_multi_output([subs, ffv], 'scale2ref')
- ffv = s2r[1].overlay(s2r[0])
- opts = {
- 'c:v': 'libx264',
- 'preset': 'medium',
- 'tune': 'animation',
- 'movflags': 'faststart',
- 'crf': '28',
- 'c:a': 'aac',
- 'b:a': '160k',
- 'ac': '1',
- #'t': '60'
- }
- audio = ffin[f'a:{audio_track}']
- stream = ffmpeg.output(ffv, audio, outfile, **opts)
- print(f"will run {' '.join(stream.get_args())=}", file=sys.stderr)
- # basic flock queueing for multiple invocations
- with open("/tmp/vrcencode", "w+") as f:
- fcntl.flock(f, fcntl.LOCK_EX)
- stream.run()
Advertisement
Add Comment
Please, Sign In to add comment