arthenica / ffmpeg-kit

FFmpeg Kit for applications. Supports Android, Flutter, iOS, Linux, macOS, React Native and tvOS. Supersedes MobileFFmpeg, flutter_ffmpeg and react-native-ffmpeg.
https://arthenica.github.io/ffmpeg-kit
GNU Lesser General Public License v3.0
4.55k stars 605 forks source link

Is there any way to pause and resume the conversion process? #986

Closed fsmdeveloper closed 1 month ago

fsmdeveloper commented 6 months ago

I want to pause the process, and then resume it later. is it possible?? if possible then please suggest me how can I do that?

AnshulRaghav commented 5 months ago

@fsmdeveloper Could you share the command with which you want to use this behaviour and the reason for doing so(optimisation,etc)?

fsmdeveloper commented 5 months ago

I want to start the command, then pause it, and resume it. I mean compressing video. mp4 to mkv with resume support!

AnshulRaghav commented 5 months ago

While FFmpeg does not have built-in pause and resume functionality for encoding, you can achieve similar results by splitting the encoding process into parts and then concatenating the results, or by using segmented intermediate files. This approach requires some manual intervention and management but allows you to effectively pause and resume the encoding process.

Example Using Split Files Step 1: Initial Encoding Encode the video until a certain point and stop it: ffmpeg -i input.mp4 -t 00:05:00 -c copy part1.mp4 This command stops encoding at 5 minutes (00:05:00).

Step 2: Encoding from Stopped Point Start encoding from where you left off: ffmpeg -ss 00:05:00 -i input.mp4 -c copy part2.mp4 This command starts encoding from 5 minutes onward.

Step 3: Concatenating the Parts Concatenate the encoded parts to form a complete video: ffmpeg -f concat -safe 0 -i <(echo "file 'part1.mp4'"; echo "file 'part2.mp4'") -c copy output.mp4

Example Using Intermediate Files (Segmenting) This method involves segmenting the output into smaller chunks that can be easily resumed.

Step 1: Start Encoding with Segmenting ffmpeg -i input.mp4 -c copy -f segment -segment_time 300 -reset_timestamps 1 out%03d.mp4 This command segments the output into 5-minute chunks (segment_time 300).

Step 2: Stop and Resume Stop the process when you want to pause and resume by running the command again. Ensure the output files continue from the last segment: ffmpeg -ss <timestamp> -i input.mp4 -c copy -f segment -segment_time 300 -reset_timestamps 1 out%03d.mp4 You will need to determine the correct timestamp to resume from.

For encoding videos to different formats: ffmpeg -ss <timestamp> -i input.mp4 -c:v libx264 -c:a aac -f segment -segment_time 300 -reset_timestamps 1 -g 30 -sc_threshold 0 out%03d.mp4 Replace timestamp with the appropriate start time.

-c:v libx264 specifies the video codec. -c:a aac specifies the audio codec. -f segment enables segmenting. _-segmenttime 300 sets the segment duration to 5 minutes. _-resettimestamps 1 resets timestamps for each segment. -g 30 sets the GOP (Group of Pictures) size, ensuring keyframes are aligned with segment boundaries. _-scthreshold 0 disables scene change detection to ensure segments are consistent.

fsmdeveloper commented 5 months ago

Thank you so much for your approach! I'll try it!

github-actions[bot] commented 2 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 1 month ago

This issue was closed because it has been stalled for 7 days with no activity.