-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add new remuxer to normalize media within milliseconds #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bajankristof
wants to merge
1
commit into
main
Choose a base branch
from
feat/remux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'fileutils' | ||
| require 'tmpdir' | ||
|
|
||
| require_relative 'media' | ||
|
|
||
| module FFMPEG | ||
| # The Remuxer class is responsible for remuxing multimedia files via stream copy. | ||
| # It attempts a direct stream copy first, and if that fails (e.g. due to corrupted | ||
| # timestamps), it falls back to extracting raw Annex B streams and re-muxing them | ||
| # with a corrected frame rate. | ||
| # | ||
| # @example | ||
| # remuxer = FFMPEG::Remuxer.new | ||
| # status = remuxer.process('input.mp4', 'output.mp4') | ||
| # status.success? # => true | ||
| class Remuxer | ||
| ANNEXB_CODEC_NAMES = %w[h264 hevc].freeze | ||
|
|
||
| # @param name [String, nil] An optional name for the remuxer. | ||
| # @param metadata [Hash, nil] Optional metadata to associate with the remuxer. | ||
| # @param checks [Array<Symbol, Proc>] Checks to run on the output to determine success. | ||
| # @param timeout [Integer, nil] Timeout in seconds for each ffmpeg command. | ||
| def initialize(name: nil, metadata: nil, checks: %i[exist?], timeout: nil) | ||
| @name = name | ||
| @metadata = metadata | ||
| @checks = checks | ||
| @timeout = timeout | ||
| end | ||
|
|
||
| class << self | ||
| # Returns true if the media has a video codec that supports lossless | ||
| # Annex B bitstream extraction (H.264 or H.265). | ||
| # | ||
| # @param media [FFMPEG::Media] | ||
| # @return [Boolean] | ||
| def annexb?(media) | ||
| media.video? && ANNEXB_CODEC_NAMES.include?(media.video_codec_name) | ||
| end | ||
| end | ||
|
|
||
| # Remuxes the media file to the given output path via stream copy. | ||
| # If the initial stream copy fails and the video codec supports Annex B | ||
| # extraction, it falls back to extracting raw streams and re-muxing with | ||
| # a corrected frame rate. | ||
| # | ||
| # @param media [String, Pathname, URI, FFMPEG::Media] The media file to remux. | ||
| # @param output_path [String, Pathname] The output path for the remuxed file. | ||
| # @yield [report] Reports from the ffmpeg command (see FFMPEG::Reporters). | ||
| # @return [FFMPEG::Transcoder::Status] | ||
| def process(media, output_path, &) | ||
| media = Media.new(media, load: false) unless media.is_a?(Media) | ||
|
|
||
| status = ffmpeg_copy(media, output_path, &) | ||
| return status if status.success? | ||
| return status unless self.class.annexb?(media) | ||
|
|
||
| Dir.mktmpdir do |tmpdir| | ||
| annexb_extname = media.video_codec_name == 'hevc' ? '.h265' : '.h264' | ||
| annexb_path = File.join(tmpdir, "remux#{annexb_extname}") | ||
| annexb_filter = annexb_filter(media) | ||
| annexb_status = ffmpeg_copy(media, '-map', '0:v:0', *annexb_filter, annexb_path, &) | ||
| return annexb_status unless annexb_status.success? | ||
|
|
||
| mka_path = File.join(tmpdir, 'remux.mka') | ||
| mka_status = ffmpeg_copy(media, '-vn', mka_path, &) | ||
| return mka_status unless mka_status.success? | ||
|
|
||
| video = annexb_status.media.first | ||
| audio = mka_status.media.first | ||
| frame_rate = detect_frame_rate(video, audio) | ||
|
|
||
| status = ffmpeg_copy( | ||
| [video, audio, media], | ||
| '-map', '0:v', | ||
| '-map', '1:a', | ||
| '-map_metadata', '2', | ||
| output_path, | ||
| inargs: %W[-r #{frame_rate}], | ||
| & | ||
| ) | ||
| return status unless status.success? | ||
| return status unless FFMPEG.exiftool_binary | ||
|
|
||
| FFMPEG.exiftool_capture3( | ||
| '-overwrite_original', | ||
| "-rotation=#{media.rotation}", | ||
| output_path | ||
| ).tap do |_, stderr, exiftool_status| | ||
| next if exiftool_status.success? | ||
|
|
||
| status.warn!("ExifTool exited with non-zero status: #{exiftool_status.exitstatus}\n#{stderr.strip}") | ||
| end | ||
|
|
||
| status | ||
| end | ||
| end | ||
|
|
||
| # Remuxes the media file to the given output path via stream copy, | ||
| # raising an error if the remux fails. | ||
| # | ||
| # @param media [String, Pathname, URI, FFMPEG::Media] The media file to remux. | ||
| # @param output_path [String, Pathname] The output path for the remuxed file. | ||
| # @yield [report] Reports from the ffmpeg command (see FFMPEG::Reporters). | ||
| # @return [FFMPEG::Transcoder::Status] | ||
| def process!(media, output_path, &) | ||
| process(media, output_path, &).assert! | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def ffmpeg_copy(media, *args, inargs: [], &) | ||
| media = [media] unless media.is_a?(Array) | ||
|
|
||
| FFMPEG.ffmpeg_execute( | ||
| *inargs.map(&:to_s), | ||
| *media.map { ['-i', _1.path.to_s] }.flatten, | ||
| '-c', | ||
| 'copy', | ||
| *args.map(&:to_s), | ||
| timeout: @timeout, | ||
| status: Transcoder::Status.new([args.last], checks: @checks), | ||
| & | ||
| ) | ||
| end | ||
|
|
||
| def annexb_filter(media) | ||
| ['-bsf:v', "#{media.video_codec_name}_mp4toannexb"] | ||
| end | ||
|
|
||
| def detect_frame_rate(video, audio) | ||
| stdout, = FFMPEG.ffprobe_capture3( | ||
| '-v', 'quiet', | ||
| '-count_packets', | ||
| '-select_streams', 'v:0', | ||
| '-show_entries', 'stream=nb_read_packets', | ||
| '-of', 'csv=p=0', | ||
| video.path | ||
| ) | ||
| frame_count = stdout.strip.to_i | ||
|
|
||
| stdout, = FFMPEG.ffprobe_capture3( | ||
| '-v', 'quiet', | ||
| '-show_entries', 'format=duration', | ||
| '-of', 'csv=p=0', | ||
| audio.path | ||
| ) | ||
| duration = stdout.strip.to_f | ||
|
|
||
| (frame_count.to_f / duration).round | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the
pathis not a String? if we plan to handlenil, then please add it to the method docs. if it's just a sanity check to make sure theFile.executable?works, then add a proper error handling