
Recherche avancée
Médias (91)
-
Richard Stallman et le logiciel libre
19 octobre 2011, par
Mis à jour : Mai 2013
Langue : français
Type : Texte
-
Stereo master soundtrack
17 octobre 2011, par
Mis à jour : Octobre 2011
Langue : English
Type : Audio
-
Elephants Dream - Cover of the soundtrack
17 octobre 2011, par
Mis à jour : Octobre 2011
Langue : English
Type : Image
-
#7 Ambience
16 octobre 2011, par
Mis à jour : Juin 2015
Langue : English
Type : Audio
-
#6 Teaser Music
16 octobre 2011, par
Mis à jour : Février 2013
Langue : English
Type : Audio
-
#5 End Title
16 octobre 2011, par
Mis à jour : Février 2013
Langue : English
Type : Audio
Autres articles (98)
-
Soumettre améliorations et plugins supplémentaires
10 avril 2011Si vous avez développé une nouvelle extension permettant d’ajouter une ou plusieurs fonctionnalités utiles à MediaSPIP, faites le nous savoir et son intégration dans la distribution officielle sera envisagée.
Vous pouvez utiliser la liste de discussion de développement afin de le faire savoir ou demander de l’aide quant à la réalisation de ce plugin. MediaSPIP étant basé sur SPIP, il est également possible d’utiliser le liste de discussion SPIP-zone de SPIP pour (...) -
MediaSPIP v0.2
21 juin 2013, parMediaSPIP 0.2 est la première version de MediaSPIP stable.
Sa date de sortie officielle est le 21 juin 2013 et est annoncée ici.
Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
Comme pour la version précédente, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...) -
MediaSPIP version 0.1 Beta
16 avril 2011, parMediaSPIP 0.1 beta est la première version de MediaSPIP décrétée comme "utilisable".
Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
Pour avoir une installation fonctionnelle, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...)
Sur d’autres sites (17218)
-
Could anyone help me understand why moviepy is rendering at 2.5 it/s ?
23 décembre 2023, par tristanI'm writing a program that uses moviepy to make those weird reddit thread videos with mc parkour playing in the background (real original ik), and everything is good except for when im rendering video which seems to consume a ton of memory and moves really... really slow, like 2.5 it/s. could anyone help ? also im a novice programmer that has no bearing on what is conventional or proper, so sorry if my code is very bad.


from moviepy.video.fx.all import resize
from moviepy.video.tools.subtitles import SubtitlesClip
from moviepy.editor import (
 CompositeVideoClip,
 AudioFileClip,
 VideoFileClip,
 ImageClip,
 TextClip
)
import random
import moviepy.config as cfg
import librosa
from imagegenerator import draw_title
from audioeditor import concatenate_audios
import soundfile as sf
import numpy as np

# Constants
VIDEO_FADE_DURATION = 0.4
SPEED_FACTOR = 1.1
TEXT_WIDTH = 600
MINIMUM_FONT_SIZE = 60
FONT_COLOR = "white"
OUTLINE_COLOR = "black"
TITLE_ANIMATION_DURATION = 0.25
ANIMATION_DURATION = 0.2

# Configure imagemagick binary
cfg.change_settings(
 {
 "IMAGEMAGICK_BINARY": "magick/magick.exe"
 }
)

# Ease-out function
def ease_out(t):
 return 1 - (1 - t) ** 2

# Overlap audio files
def overlap_audio_files(audio_path1, audio_path2):
 # Load the first audio file
 audio1, sr1 = librosa.load(audio_path1, sr=None)

 # Load the second audio file
 audio2, sr2 = librosa.load(audio_path2, sr=None)

 # Ensure both audio files have the same sample rate
 if sr1 != sr2:
 raise ValueError("Sample rates of the two audio files must be the same.")

 # Calculate the duration of audio2
 audio2_duration = len(audio2)

 # Tile audio1 to match the duration of audio2
 audio1 = np.tile(audio1, int(np.ceil(audio2_duration / len(audio1))))

 # Trim audio1 to match the duration of audio2
 audio1 = audio1[:audio2_duration]

 # Combine the audio files by superimposing them
 combined_audio = audio1 + audio2

 # Save the combined audio to a new file
 output_path = "temp/ttsclips/combined_audio.wav"
 sf.write(output_path, combined_audio, sr1)

 return output_path

# Generator function for subtitles with centered alignment and outline
def centered_text_generator_white(txt):
 return TextClip(
 txt,
 font=r"fonts/Invisible-ExtraBold.otf",
 fontsize=86,
 color=FONT_COLOR,
 bg_color='transparent', # Use a transparent background
 align='center', # Center the text
 size=(1072, 1682),
 method='caption', # Draw a caption instead of a title
 )

# Generator function for subtitles with centered alignment and blurred outline
def centered_text_generator_black_blurred_outline(txt, blur_factor=3):
 outline_clip = TextClip(
 txt,
 font=r"fonts/Invisible-ExtraBold.otf",
 fontsize=86,
 color=OUTLINE_COLOR,
 bg_color='transparent', # Use a transparent background
 align='center', # Center the text
 size=(1080, 1688),
 method='caption', # Draw a caption instead of a title
 )

 # Blur the black text (outline)
 blurred_outline_clip = outline_clip.fx(resize, 1.0 / blur_factor)
 blurred_outline_clip = blurred_outline_clip.fx(resize, blur_factor)

 return blurred_outline_clip

# Compile video function
def compile_video(title_content, upvotes, comments, tone, subreddit, video_num):
 # Set the dimensions of the video (720x1280 in this case)
 height = 1280

 # Concatenate the audios
 concatenate_audios()

 concatenated_audio_path = r"temp/ttsclips/concatenated_audio.mp3"
 title_audio_path = r"temp/ttsclips/title.mp3"

 title_audio = AudioFileClip(title_audio_path)
 concatenated_audio = AudioFileClip(concatenated_audio_path)

 # Calculate for video duration
 title_duration = title_audio.duration
 duration = concatenated_audio.duration

 # Set background
 background_path = f"saved_videos/newmcparkour.mp4"
 background = VideoFileClip(background_path)
 background_duration = background.duration
 random_start = random.uniform(0, background_duration - duration)
 background = background.subclip(random_start, random_start + duration)

 # Apply fade-out effect to both background clips
 background = background.crossfadeout(VIDEO_FADE_DURATION)

 # Generate the background image with rounded corners
 background_image_path = draw_title(title_content, upvotes, comments, subreddit)

 # Load the background image with rounded corners
 background_image = ImageClip(background_image_path)

 # Set the start of the animated title clip
 animated_background_clip = background_image.set_start(0)

 # Set the initial position of the text at the bottom of the screen
 initial_position = (90, height)

 # Calculate the final position of the text at the center of the screen
 final_position = [90, 630]

 # Animate the title clip to slide up over the course of the animation duration
 animated_background_clip = animated_background_clip.set_position(
 lambda t: (
 initial_position[0],
 initial_position[1]
 - (initial_position[1] - final_position[1])
 * ease_out(t / TITLE_ANIMATION_DURATION),
 )
 )

 # Set the duration of the animated title clip
 animated_background_clip = animated_background_clip.set_duration(
 TITLE_ANIMATION_DURATION
 )

 # Assign start times to title image
 stationary_background_clip = background_image.set_start(TITLE_ANIMATION_DURATION)

 # Assign positions to stationary title image
 stationary_background_clip = stationary_background_clip.set_position(final_position)

 # Assign durations to stationary title image
 stationary_background_clip = stationary_background_clip.set_duration(
 title_duration - TITLE_ANIMATION_DURATION
 )

 # Select background music
 if tone == "normal":
 music_options = [
 "Anguish",
 "Garden",
 "Limerence",
 "Lost",
 "NoWayOut",
 "Summer",
 "Never",
 "Miss",
 "Touch",
 "Stellar"
 ]
 elif tone == "eerie":
 music_options = [
 "Creepy",
 "Scary",
 "Spooky",
 "Space",
 "Suspense"
 ]
 background_music_choice = random.choice(music_options)
 background_music_path = f"music/eeriemusic/{background_music_choice}.mp3"

 # Create final audio by overlapping background music and concatenated audio
 final_audio = AudioFileClip(
 overlap_audio_files(background_music_path, concatenated_audio_path)
 )

 # Release the concatenated audio
 concatenated_audio.close()

 # Create subtitles clip using the centered_text_generator
 subtitles = SubtitlesClip("temp/ttsclips/content_speechmarks.srt",
 lambda txt: centered_text_generator_white(txt))
 subtitles_outline = SubtitlesClip("temp/ttsclips/content_speechmarks.srt",
 lambda txt: centered_text_generator_black_blurred_outline(txt))

 # Overlay subtitles on the blurred background
 final_clip = CompositeVideoClip(
 [background, animated_background_clip, stationary_background_clip, subtitles_outline, subtitles]
 )

 # Set the final video dimensions and export the video
 final_clip = final_clip.set_duration(duration)
 final_clip = final_clip.set_audio(final_audio)

 final_clip.write_videofile(
 f"temp/videos/{video_num}.mp4",
 codec="libx264",
 fps=60,
 bitrate="8000k",
 audio_codec="aac",
 audio_bitrate="192k",
 preset="ultrafast",
 threads=8
 )

 # Release the concatenated audio
 concatenated_audio.close()

 # Release the title audio
 title_audio.close()

 # Release the background video and image
 background.close()
 background_image.close()

 # Release the final audio
 final_audio.close()

 # Release the subtitle clips
 subtitles.close()
 subtitles_outline.close()

 # Release the final video clip
 final_clip.close()



ive tried turning down my settings, like setting it to "ultrafast" and dropping the bitrate, but nothing seems to work. the only thing I can think of now is that there is something Im doing wrong with moviepy.


-
ffmpeg concat skips frames near end of each subclip
3 janvier 2024, par calvinusesyourcodeSo I have some video files to concat but in the resulting video the last few frames of each subclip are buggy. Imagine in the last 5 frames the first 3 frames are skipped and so at the end of each clip it seems to jitter.


It should be virtually impossible for my input videos to have any differences between them, as they were all recorded on the same iPhone and all converted with the same command :


command = [
 'ffmpeg', '-y',
 '-i', input_path,
 '-vf', 'scale=1080:1920',
 '-r', '30',
 '-c:v', 'libx264',
 output_path
 ]
subprocess.run(command, check=True)



I have tried re-encoding instead of merely copying and adding
-r 30
but that doesn't seem to work.

subprocess.run(["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", temp_textfile, "-c", "copy", output_path])



subprocess.run(["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", temp_textfile, "-r", "30", "-c:v", "libx264", "-c:a", "aac", output_path], check=True)



Somewhere someone said to open in VLC and do a frame-by-frame, reporting that "the frames are actually there, just not visually when watching normally". In my case the frame-by-frame reveals the frames are indeed being skipped.


Full console output :


ffmpeg version 2023-05-18-git-01d9a84ef5-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
 built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
 configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa 
--enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
 libavutil 58. 7.100 / 58. 7.100
 libavcodec 60. 14.100 / 60. 14.100
 libavformat 60. 5.100 / 60. 5.100
 libavdevice 60. 2.100 / 60. 2.100
 libavfilter 9. 8.100 / 9. 8.100
 libswscale 7. 2.100 / 7. 2.100
 libswresample 4. 11.100 / 4. 11.100
 libpostproc 57. 2.100 / 57. 2.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d60610ebc0] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'run\broll_subclips.txt':
 Duration: N/A, start: -0.023220, bitrate: 3094 kb/s
 Stream #0:0(und): Video: h264 (High 10) (avc1 / 0x31637661), yuv420p10le(tv, bt2020nc/bt2020/arib-std-b67, progressive), 1080x1920, 2968 kb/s, 30 fps, 30 tbr, 15360 tbn
 Metadata:
 handler_name : Core Media Video
 vendor_id : [0][0][0][0]
 encoder : Lavc60.14.100 libx264
 Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 126 kb/s
 Metadata:
 handler_name : Core Media Audio
 vendor_id : [0][0][0][0]
Stream mapping:
 Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
 Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 000001d6066fd380] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX 
FMA3 BMI2 AVX2
[libx264 @ 000001d6066fd380] profile High 10, level 4.0, 4:2:0, 10-bit
[libx264 @ 000001d6066fd380] 264 - core 164 r3107 a8b68eb - H.264/MPEG-4 AVC codec - Copyleft 2003-2023 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=81 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'joined_clips.mp4':
 Metadata:
 encoder : Lavf60.5.100
 Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p10le(tv, bt2020nc/bt2020/arib-std-b67, progressive), 1080x1920, q=2-31, 30 fps, 15360 tbn
 Metadata:
 handler_name : Core Media Video
 vendor_id : [0][0][0][0]
 encoder : Lavc60.14.100 libx264
 Side data:
 cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
 Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
 Metadata:
 handler_name : Core Media Audio
 vendor_id : [0][0][0][0]
 encoder : Lavc60.14.100 aac
frame= 0 fps=0.0 q=0.0 size= 0kB time=00:00:00.23 bitrate= 1.7kbits/s dup[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 0 fps=0.0 q=0.0 size= 0kB time=00:00:01.97 bitrate= 0.2kbits/s dupframe= 42 fps= 41 q=41.0 size= 256kB time=00:00:01.97 bitrate=1062.7kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 81 fps= 53 q=41.0 size= 768kB time=00:00:04.71 bitrate=1334.8kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 124 fps= 61 q=41.0 size= 1280kB time=00:00:06.10 bitrate=1717.1kbits/s duframe= 178 fps= 69 q=38.0 size= 1536kB time=00:00:07.96 bitrate=1579.9kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 235 fps= 76 q=41.0 size= 2048kB time=00:00:09.84 bitrate=1704.1kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 273 fps= 76 q=41.0 size= 2560kB time=00:00:11.12 bitrate=1885.5kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 309 fps= 75 q=41.0 size= 2816kB time=00:00:12.30 bitrate=1874.5kbits/s duframe= 354 fps= 77 q=41.0 size= 3328kB time=00:00:13.83 bitrate=1969.9kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 411 fps= 80 q=41.0 size= 3840kB time=00:00:15.72 bitrate=2001.1kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 479 fps= 85 q=41.0 size= 4096kB time=00:00:17.99 bitrate=1864.6kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d608de8100] Auto-inserting h264_mp4toannexb bitstream filter
frame= 515 fps= 84 q=41.0 size= 4608kB time=00:00:19.20 bitrate=1965.7kbits/s duframe= 549 fps= 81 q=41.0 size= 4864kB time=00:00:20.31 bitrate=1961.1kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d60c3e8d40] Auto-inserting h264_mp4toannexb bitstream filter
frame= 600 fps= 83 q=41.0 size= 5632kB time=00:00:22.03 bitrate=2093.7kbits/s du[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d60c3e8d40] Auto-inserting h264_mp4toannexb bitstream filter
frame= 648 fps= 83 q=41.0 size= 5888kB time=00:00:23.61 bitrate=2042.5kbits/s du[out#0/mp4 @ 000001d6061163c0] video:6385kB audio:335kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.377336%
frame= 724 fps= 86 q=-1.0 Lsize= 6746kB time=00:00:24.03 bitrate=2299.4kbits/s dup=6 drop=2 speed=2.86x
[libx264 @ 000001d6066fd380] frame I:12 Avg QP:26.96 size: 87427
[libx264 @ 000001d6066fd380] frame P:191 Avg QP:32.28 size: 15534
[libx264 @ 000001d6066fd380] frame B:521 Avg QP:35.40 size: 4840
[libx264 @ 000001d6066fd380] consecutive B-frames: 2.9% 2.8% 2.1% 92.3%
[libx264 @ 000001d6066fd380] mb I I16..4: 21.0% 56.5% 22.5%
[libx264 @ 000001d6066fd380] mb P I16..4: 1.9% 6.3% 1.0% P16..4: 25.0% 4.5% 2.2% 0.0% 0.0% skip:59.1%
[libx264 @ 000001d6066fd380] mb B I16..4: 0.2% 0.7% 0.1% B16..8: 24.4% 1.8% 0.2% direct: 0.3% skip:72.3% L0:46.0% L1:50.7% BI: 3.3%
[libx264 @ 000001d6066fd380] 8x8 transform intra:64.9% inter:79.1%
[libx264 @ 000001d6066fd380] coded y,uvDC,uvAC intra: 42.4% 26.9% 3.0% inter: 3.7% 0.9% 0.0%
[libx264 @ 000001d6066fd380] i16 v,h,dc,p: 25% 28% 12% 35%
[libx264 @ 000001d6066fd380] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 18% 23% 5% 6% 6% 
 6% 6% 6%
[libx264 @ 000001d6066fd380] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 21% 19% 5% 6% 6% 
 7% 5% 5%
[libx264 @ 000001d6066fd380] i8c dc,h,v,p: 71% 13% 13% 4%
[libx264 @ 000001d6066fd380] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 000001d6066fd380] ref P L0: 68.7% 17.8% 13.4%
[libx264 @ 000001d6066fd380] ref B L0: 88.4% 9.1% 2.5%
[libx264 @ 000001d6066fd380] ref B L1: 96.9% 3.1%
[libx264 @ 000001d6066fd380] kb/s:2167.24
[aac @ 000001d606184b40] Qavg: 346.828



UPDATE : I am thinking that the way I am converting my files from .mov to .mp4 is the problem. Please suggest the best way to convert from iPhone 4k 60fps .mov files to nice 1080p 30fps .mp4 files. I know I could just use handbrake but I am trying to be a man here xD. Perhaps handbrake has a
View ffmpeg code for conversion
.

UPDATE 2 : re-encoding the videos before concat with
-c:v libx264
fixes the problem... which seems weird because that is how they were originally encoded...

def join_broll(video_paths, desired_length, clip_length=None, output_path="quick_clips.mp4", preserve_inputs=True):
 subclips = []
 total_duration = 0

 temp_textfile = os.path.join(run_folder, "broll_subclips.txt")
 j = 0
 with open(temp_textfile, "w") as file:
 while True:
 for i, video_path in enumerate(video_paths):

 time_left = desired_length - total_duration
 video_duration = duration_of(video_path)
 subclip_path = f"subclip_{i+j}.mp4"

 if (not clip_length and video_duration < time_left) or (clip_length and clip_length < time_left):

 if clip_length:

 subclips.append(subclip_path)
 subprocess.run(["ffmpeg", "-y", "-i", video_path, "-t", str(clip_length), "-c:v", "libx264", subclip_path]) # added "-c:v libx264"
 total_duration += clip_length
 file.write(f"file '{os.path.join('..', subclip_path)}'\n")

 else:
 
 subclips.append(subclip_path)
 subprocess.run(["ffmpeg", "-y", "-i", video_path, "-c:v", "libx264", subclip_path]) # added "-c:v libx264"
 total_duration += video_duration
 file.write(f"file '{subclip_path}'\n")

 else:

 subclips.append(subclip_path)
 subprocess.run(["ffmpeg", "-y", "-i", video_path, "-t", str(time_left), "-c:v", "libx264", subclip_path]) # added "-c:v libx264"
 total_duration += time_left
 file.write(f"file '{os.path.join('..', subclip_path)}'\n")

 break

 j += 1
 if desired_length - total_duration < 0.1:
 break
 

 subprocess.run(["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", temp_textfile, "-c", "copy", output_path])
 # subprocess.run(["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", temp_textfile, "-r", "30", "-c:v", "libx264", "-c:a", "aac", output_path], check=True)
 return output_path



-
How to convert multiple jpg images to MP4 format using FFmpeg ?
30 janvier 2024, par PDLAOZEI- 

-
I first used the FFmpeg command line to implement this function.

ffmpeg -framerate 1 -pattern_type glob -i "./image/*.jpg" -c:v libx264 -crf 25 -vf format=yuv420p -movflags +faststart test.mp4


-
Then I use FFmpeg's api to convert multiple jpg into MP4 format, the process is probably.








- 

- Find the AV_CODEC_ID_MPEG4 encoder.
- Turn on the encoder.
- Set video stream parameters.
- Open the output file.
- Write file header.
- Allocate image frame rate and buffer.
- Write each jpg image to the video (write in a loop).
7-1. read jpg image.
7-2. Find video stream information.
7-3. Decoded image frame.
7-4. Transcode to yuv420p.
7-5. Encode and write video frames.
- Finally, free memory.


















update the reproducible example


#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <thread>

#include <filesystem>
#include <algorithm>
#include <iomanip>
#include <sys></sys>stat.h>

extern "C"
{
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libswscale></libswscale>swscale.h>
#include <libavutil></libavutil>imgutils.h>
}

namespace fs = std::filesystem;

bool compareFilenames(const std::string &str1, const std::string &str2)
{
 //ToDo Sort
}

int main()
{
 
 fs::path folderPath = "" //replace your image foldePath

 std::vector imageFiles;
 for (const auto &entry : fs::directory_iterator(folderPath))
 {
 if (entry.is_regular_file() && entry.path().extension() == ".jpg")
 {
 imageFiles.push_back(entry.path());
 }
 }

 std::sort(imageFiles.begin(), imageFiles.end(), compareFilenames);

 std::string outputVideoFile = "output.mp4";

 AVFormatContext *formatContext = nullptr;
 AVCodecContext *codecContext = nullptr;
 AVStream *videoStream = nullptr;

 av_register_all();
 avformat_alloc_output_context2(&formatContext, nullptr, nullptr, outputVideoFile.c_str());


 AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);//AV_CODEC_ID_H264
 videoStream = avformat_new_stream(formatContext, codec);
 codecContext = avcodec_alloc_context3(codec);

 codecContext->codec_id = codec->id;
 codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
 codecContext->pix_fmt = AV_PIX_FMT_YUV420P;
 codecContext->width = 1920;
 codecContext->height = 1080;
 codecContext->time_base = {1, 2}; 
 codecContext->bit_rate = 100000000;
 codecContext->gop_size = 1;

 avcodec_open2(codecContext, codec, nullptr);
 avcodec_parameters_from_context(videoStream->codecpar, codecContext);

 avio_open(&formatContext->pb, outputVideoFile.c_str(), AVIO_FLAG_WRITE);
 avformat_write_header(formatContext, NULL);

 AVFrame *frame = av_frame_alloc();

 std::vector imageBuffer(codecContext->width * codecContext->height * 3);
 AVPacket packet;

 for (const std::string &imageFile : imageFiles)
 {

 AVFormatContext *inputFormatContext = avformat_alloc_context();
 avformat_open_input(&inputFormatContext, imageFile.c_str(), nullptr, nullptr);

 avformat_find_stream_info(inputFormatContext, nullptr);

 AVCodec *inputCodec = nullptr;
 AVCodecContext *inputCodecContext = nullptr;
 int videoStreamIndex = -1;

 for (unsigned int i = 0; i < inputFormatContext->nb_streams; i++)
 {
 if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
 {
 videoStreamIndex = i;
 inputCodec = avcodec_find_decoder(inputFormatContext->streams[i]->codecpar->codec_id);
 inputCodecContext = avcodec_alloc_context3(inputCodec);
 avcodec_parameters_to_context(inputCodecContext, inputFormatContext->streams[i]->codecpar);
 avcodec_open2(inputCodecContext, inputCodec, NULL);
 break;
 }
 }

 AVFrame *inputFrame = av_frame_alloc();
 while (av_read_frame(inputFormatContext, &packet) >= 0)
 {
 if (packet.stream_index == videoStreamIndex)
 {
 int response = avcodec_send_packet(inputCodecContext, &packet);

 while (response >= 0)
 {
 response = avcodec_receive_frame(inputCodecContext, inputFrame);
 if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)
 {
 break;
 }
 else if (response < 0)
 {
 std::cerr << "Failed to receive frame from input decoder" << std::endl;
 break;
 }

 AVFrame *yuvFrame = av_frame_alloc();
 av_image_alloc(yuvFrame->data, yuvFrame->linesize, codecContext->width, codecContext->height, codecContext->pix_fmt, 1);

 SwsContext *yuvSwsContext = sws_getContext(inputFrame->width, inputFrame->height, AV_PIX_FMT_RGB24,
 codecContext->width, codecContext->height, codecContext->pix_fmt,
 0, nullptr, nullptr, nullptr);

 sws_scale(yuvSwsContext, inputFrame->data, inputFrame->linesize, 0, inputFrame->height,
 yuvFrame->data, yuvFrame->linesize);


 memcpy(imageBuffer.data(), yuvFrame->data[0], codecContext->width * codecContext->height);
 memcpy(imageBuffer.data() + codecContext->width * codecContext->height, yuvFrame->data[1], codecContext->width * codecContext->height / 4);
 memcpy(imageBuffer.data() + codecContext->width * codecContext->height * 5 / 4, yuvFrame->data[2], codecContext->width * codecContext->height / 4);

 frame->data[0] = imageBuffer.data();
 frame->data[1] = frame->data[0] + codecContext->width * codecContext->height;
 frame->data[2] = frame->data[1] + codecContext->width * codecContext->height / 4;
 frame->linesize[0] = codecContext->width;
 frame->linesize[1] = codecContext->width / 2;
 frame->linesize[2] = codecContext->width / 2;
 frame->width = codecContext->width;
 frame->height = codecContext->height;
 frame->format = codecContext->pix_fmt;
 frame->pts = av_rescale_q(videoStream->nb_frames, videoStream->time_base, videoStream->codec->time_base);

 av_init_packet(&packet);
 packet.data = nullptr;
 packet.size = 0;

 avcodec_send_frame(codecContext, frame);

 while (avcodec_receive_packet(codecContext, &packet) >= 0)
 {
 av_packet_rescale_ts(&packet, codecContext->time_base, videoStream->time_base);
 packet.stream_index = videoStream->index;

 av_write_frame(formatContext, &packet);
 av_packet_unref(&packet);
 }

 av_freep(&yuvFrame->data[0]);
 av_freep(&yuvFrame);
 sws_freeContext(yuvSwsContext);
 }
 }

 av_packet_unref(&packet);
 }

 av_frame_free(&inputFrame);
 avcodec_close(inputCodecContext);
 avformat_close_input(&inputFormatContext);
 }

 av_write_trailer(formatContext);

 av_frame_free(&frame);
 avcodec_close(codecContext);
 av_free(codecContext);
 avio_close(formatContext->pb);
 avformat_free_context(formatContext);

 return 0;
}


</iomanip></algorithm></filesystem></thread></chrono></string></vector></iostream>


But the encoding format is mpeg, which cannot be opened in the player included with windows, and the prompt is an unsupported format.


Although the vlc player can be opened and played normally, I need to play it normally on the player that comes with windows, because the output video will be played on the browser.


So I changed AV_CODEC_ID_MPEG4 to AV_CODEC_ID_H264, and the result is that although the file size of the output image is very large, only one picture is displayed during playback. I don't know what the problem is.


I was thinking about writing a new one, but I had no idea how to write a new one.
I would appreciate it if someone could write me an example.


-