2

How can I split a video into frames and then reassemble it with the audio too?

I'm using this:

To extract the frames: ffmpeg -i "video_path" frame%04d.png

To extract the audio: ffmpeg -i video_path -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio_path_output

To reassemble: ffmpeg -i img%04d.png -i "audio_path" -c:v libx264 -r 25 -pix_fmt yuv420p -c:a copy -shortest "video_path_output"

1
  • Assuming the video_path is MP4, it's better to keep the audio without re-encoding: ffmpeg -vn -i video_path.mp4 -c copy audio_path_output.mp4. In the last command place the -r 25 before -i img%04d.png (with 25 it doesn't meter, but for other fps it does). What is the question? Isn't it working? Are you getting out of sync audio?
    – Rotem
    Commented Dec 17, 2022 at 21:44

1 Answer 1

1

To split a video into frames and then reassemble it with the audio, you can use the ffmpeg command-line tool.

To extract the frames from the video, you can use the following command:

ffmpeg -i "video_path" frame%04d.png

This command will extract the frames from the video and save them as separate image files. The %04d in the output file name specifies the frame number, with leading zeros to ensure that the files are properly sorted.

To extract the audio from the video, you can use the following command:

ffmpeg -i video_path -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio_path_output

This command will extract the audio from the video and save it as a separate MP3 file.

To reassemble the video from the frames and audio, you can use the following command:

ffmpeg -i img%04d.png -i "audio_path" -c:v libx264 -r 25 -pix_fmt yuv420p -c:a copy -shortest "video_path_output"

This command will take the frames and audio as input, and reassemble them into a new video file. The -c:v and -c:a options specify the codecs to use for the video and audio, respectively. The -r option specifies the frame rate, and the -pix_fmt option specifies the pixel format. The -shortest option ensures that the output video is the same duration as the shortest input (either the video or the audio).

To merge the three commands into a single script, you can use the following shell script:

    # Extract frames from video
ffmpeg -i "video_path" frame%04d.png

# Extract audio from video
ffmpeg -i video_path -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio_path_output

# Reassemble video from frames and audio
ffmpeg -i img%04d.png -i "audio_path" -c:v libx264 -r 25 -pix_fmt yuv420p -c:a copy -shortest "video_path_output"

You can save this script to a file (e.g. split_and_reassemble.sh) and run it using the following command:

bash split_and_reassemble.sh

This will execute the three commands in sequence, extracting the frames from the video, extracting the audio, and then reassembling the video from the frames and audio.

1
  • does the frame rate depend on the original? would -r 25 use all the frames?
    – yarns
    Commented Jul 13 at 20:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .